我想獲取日期,該日期是兩個日期之間的月份的第三個星期六。我做了,但不是我想要的。獲取開始日期和結束日期之間的月份的第3個星期六
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
發表您的日誌.. – Simmy
@Sneha我編輯我的職務原木 –
打印的currentdate和TODATE到的NSLog和展示它太.. – Simmy