【问题标题】:Get 3rd Saturday of the month between startdate and enddate在开始日期和结束日期之间获取每月的第三个星期六
【发布时间】:2016-10-18 18:36:25
【问题描述】:

我想获取两个日期之间每月第三个星期六的日期。我已经完成了,但这不是我想要的。

NSInteger count = 0;
NSInteger saturday = 7;

// Set the incremental interval for each interaction.
NSDateComponents *oneDay = [[NSDateComponents alloc] init];
[oneDay setDay:1];

// Using a Gregorian calendar.
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDate *currentDate = [[UtilityClass sharedObject] stringToDate:@"2016-06-17" withFormate:@"yyyy-MM-dd"];
        NSDate *toDate = [[UtilityClass sharedObject] stringToDate:@"2016-08-17" withFormate:@"yyyy-MM-dd"];


// Iterate from fromDate until toDate
while ([currentDate compare:toDate] == NSOrderedAscending) {

NSDateComponents *dateComponents = [calendar components:NSWeekdayCalendarUnit fromDate:currentDate];
[dateComponents setWeekdayOrdinal:3];

if (dateComponents.weekday == saturday) {
    count++;
    NSLog(@"Date = %@",currentDate);
}

// "Increment" currentDate by one day.
currentDate = [calendar dateByAddingComponents:oneDay
                                                    toDate:currentDate
                                                   options:0];
}

在我的代码中,我已成功获得星期六,但所有星期六都在 startdateenddate 之间。 我也试过这个:

[dateComponents setWeekdayOrdinal:3]; 但不工作。

这是我的日志

2016-06-17 11:29:18.319 Floacstation[1715:84848] Current Date :2016-06-16 18:30:00 +0000
2016-06-17 11:29:18.319 Floacstation[1715:84848] To Date : 2016-08-16 18:30:00 +0000

2016-06-17 11:29:18.320 Floacstation[1715:84848] Saturday Date = 2016-06-19 18:30:00 +0000
2016-06-17 11:29:18.320 Floacstation[1715:84848] Saturday Date = 2016-06-26 18:30:00 +0000
2016-06-17 11:29:18.320 Floacstation[1715:84848] Saturday Date = 2016-07-03 18:30:00 +0000
2016-06-17 11:29:18.320 Floacstation[1715:84848] Saturday Date = 2016-07-10 18:30:00 +0000
2016-06-17 11:29:18.320 Floacstation[1715:84848] Saturday Date = 2016-07-17 18:30:00 +0000
2016-06-17 11:29:18.320 Floacstation[1715:84848] Saturday Date = 2016-07-24 18:30:00 +0000
2016-06-17 11:29:18.320 Floacstation[1715:84848] Saturday Date = 2016-07-31 18:30:00 +0000
2016-06-17 11:29:18.321 Floacstation[1715:84848] Saturday Date = 2016-08-07 18:30:00 +0000
2016-06-17 11:29:18.321 Floacstation[1715:84848] Saturday Date = 2016-08-14 18:30:00 +0000

【问题讨论】:

  • @Sneha 我用日志编辑了我的帖子
  • 将 currentDate 和 toDate 打印到 NSLog 中并显示出来..
  • 现在完成我记录当前日期和日期
  • 检查我的答案....
  • 谢谢,但不工作:(

标签: ios objective-c iphone nsdate nsdatecomponents


【解决方案1】:

原因:

请注意,您输入了日期17th,但它返回给您16th.. 因为它正在考虑UTC 格式,只要您是printing NSDate in NSLogconverting it explicitly to NSString

只要你想要字符串中的日期,就使用我在本代码中使用的NSDateFormatter..

您所面临的问题正是因为如此。 所以,无论你在哪里使用日期,只需将replace the method of converting date 放入字符串并再次检查输出..

试试这个……它一定会给你正确的答案……

NSInteger count = 0;
NSInteger saturday = 7;

// Set the incremental interval for each interaction.
NSDateComponents *oneDay = [[NSDateComponents alloc] init];
[oneDay setDay:1];

// Using a Gregorian calendar.
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDate *currentDate = [[UtilityClass sharedObject] stringToDate:@"2016-06-17" withFormate:@"yyyy-MM-dd"];
        NSDate *toDate = [[UtilityClass sharedObject] stringToDate:@"2016-08-17" withFormate:@"yyyy-MM-dd"];


// Iterate from fromDate until toDate
while ([currentDate compare:toDate] == NSOrderedAscending) {

NSDateComponents *dateComponents = [calendar components:NSWeekdayCalendarUnit fromDate:currentDate];
[dateComponents setWeekdayOrdinal:3];

if (dateComponents.weekday == saturday) {
    count++;

    if (count==3) {
            NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
            formatter.dateFormat=@"dd MM YYYY";

            NSLog(@"Date = %@",[formatter stringFromDate:currentDate]);
        }
  }

}   

// 不需要增加这个日期..

// "Increment" currentDate by one day.
// currentDate = [calendar dateByAddingComponents:oneDay
//                                                    toDate:currentDate
//                                                   options:0];

希望这会有所帮助... :)

【讨论】:

  • 日志值当前日期:2016-06-16 18:30:00 +0000 截止日期:2016-08-16 18:30:00 +0000 日期 = 04 07 2016 在我的日历显示中今天是星期一
【解决方案2】:

NSCalendar 拥有强大的技能,无需重复循环

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *currentDate = [[UtilityClass sharedObject] stringToDate:@"2016-06-17" withFormate:@"yyyy-MM-dd"];
NSDate *toDate = [[UtilityClass sharedObject] stringToDate:@"2016-08-17" withFormate:@"yyyy-MM-dd"];

NSDateComponents *components = [[NSDateComponents alloc] init];
components.weekday = 7;
components.weekdayOrdinal = 3;
components.month = [calendar component:NSCalendarUnitMonth fromDate:currentDate];

NSDate *nextThirdSaturday = [calendar nextDateAfterDate:currentDate matchingComponents: components options: NSCalendarMatchNextTime];
if (nextThirdSaturday && [nextThirdSaturday compare:toDate] == NSOrderedAscending) {
   NSLog(@"%@", nextThirdSaturday);
} else {
   NSLog(@"Not Found");
}

编辑:

如果你想从开始日期计算第三个星期六并且在开始和结束日期的范围内使用这个

NSDateComponents *components = [[NSDateComponents alloc] init];
components.weekday = 7;

__block NSInteger count = 1;
__block NSDate *found;
[calendar enumerateDatesStartingAfterDate:currentDate matchingComponents:components options:NSCalendarMatchNextTime usingBlock:^(NSDate *date, BOOL exactMatch, BOOL *stop) {
   if (count == 3) { found = date; }
   if  (found || [date compare:toDate] == NSOrderedDescending) *stop = YES;
   count++;
}];
NSLog(@"%@", found);

【讨论】:

  • 我只得到开始日期。
  • 我在 Swift 操场上对其进行了测试,它运行良好
  • 对于给定的开始日期:一,即明天(2016-06-18)。
  • 你获得了第三名。这个月的。但我想坐第三。在开始日期和结束日期之间
  • 很遗憾,您的问题有点含糊(每月第三个星期六是明天)。好的,你期待什么结果?
【解决方案3】:

据我了解,你要的日期是2016-07-02? 试试这个:

NSDate *currentDate = [NSDate dateWithString:@"2016-06-16" formatString:@"yyyy-MM-dd"];

NSDate *toDate = [NSDate dateWithString:@"2016-08-17" formatString:@"yyyy-MM-dd"];

NSLog(@"Oh, %@ is the 3rd Saturday", [[currentDate dateByAddingWeeks:2] dateByAddingDays:(saturday - currentDate.weekday + 1)]);

我使用名为 DateTools 的第三个库。

【讨论】:

    【解决方案4】:
    NSInteger count = 0;
    NSInteger saturday = 7;
    
    // Set the incremental interval for each interaction.
    NSDateComponents *oneDay = [[NSDateComponents alloc] init];
    [oneDay setDay:1];
    
    // Using a Gregorian calendar.
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSDate *currentDate = [[UtilityClass sharedObject] stringToDate:@"2016-06-17" withFormate:@"yyyy-MM-dd"];
            NSDate *toDate = [[UtilityClass sharedObject] stringToDate:@"2016-08-17" withFormate:@"yyyy-MM-dd"];
    
    
    // Iterate from fromDate until toDate
    while ([currentDate compare:toDate] == NSOrderedAscending) {
    
    NSDateComponents *dateComponents = [calendar components:NSWeekdayCalendarUnit fromDate:currentDate];
    [dateComponents setWeekdayOrdinal:3];
    
    if (dateComponents.weekday == saturday) {
        count++;
        NSLog(@"3rd Satureday Date = %@",currentDate);
        if(count == 3)
            break;
    }
    
    // "Increment" currentDate by one day.
    currentDate = [calendar dateByAddingComponents:oneDay
                                                        toDate:currentDate
                                                       options:0];
    }
    

    这是我的日志

    2016-06-17 11:07:53.335 Floacstation[1568:75953] Date = 2016-06-17 18:30:00 +0000
    2016-06-17 11:07:53.335 Floacstation[1568:75953] Date = 2016-06-24 18:30:00 +0000
    2016-06-17 11:07:53.335 Floacstation[1568:75953] Date = 2016-07-01 18:30:00 +0000
    2016-06-17 11:07:53.335 Floacstation[1568:75953] Date = 2016-07-08 18:30:00 +0000
    2016-06-17 11:07:53.335 Floacstation[1568:75953] Date = 2016-07-15 18:30:00 +0000
    2016-06-17 11:07:53.336 Floacstation[1568:75953] Date = 2016-07-22 18:30:00 +0000
    2016-06-17 11:07:53.336 Floacstation[1568:75953] Date = 2016-07-29 18:30:00 +0000
    2016-06-17 11:07:53.336 Floacstation[1568:75953] Date = 2016-08-05 18:30:00 +0000
    2016-06-17 11:07:53.336 Floacstation[1568:75953] Date = 2016-08-12 18:30:00 +0000
    

    【讨论】:

    • nslog的值是多少?
    • 只得到一个星期六的日期,而且那个值我弄错了它不是星期六日期
    • 这是第三个星期六日期:)
    • 但我只得到一个日期,但是当我查看日历时它显示两个日期,这也是星期六
    • 它只获得周六的前 3 个日期。日志值: - 2016-06-17 2016-06-24 2016-07-01
    猜你喜欢
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多