获取startdate和enddate之间的第三个星期六

时间:2016-06-17 03:48:08

标签: ios objective-c iphone nsdate nsdatecomponents

我想获得两个日期之间的第三个星期六的日期。我做了但不是我想要的。

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];
}

在我的代码中,我已经成功地获得了星期六,但是所有星期六都在startdate和...之间。 enddate。 我也试过这个:

  

[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

4 个答案:

答案 0 :(得分:3)

<强>原因:

请注意,您输入了日期17th,但它已经返回16th ..因为只要您UTCprinting NSDate in NSLog,它就会考虑converting it explicitly to NSString格式。

  

每当你想要字符串中的日期然后使用我在本代码中使用的NSDateFormatter ..

你面临的问题仅仅是因为这个原因。 所以,无论你在哪里使用Date,只要replace the method of converting date进入String&amp;再次检查输出..

试试这个...它必须给你正确答案......

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];

希望这有助于......:)

答案 1 :(得分:1)

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");
}

修改

如果您希望从开始日期开始计算第3个星期六,并且在开始日期和结束日期范围内使用此

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);

答案 2 :(得分:0)

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

答案 3 :(得分:0)

根据我的理解,您想要的日期是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的第3个库。