获取开始日期和结束日期之间的月份的第三个星期六
Get 3rd Saturday of the month between startdate and enddate
我想获取两个日期之间的月份的第三个星期六的日期。我做到了,但这不是我想要的。
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];
but not working.
这是我的日志
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
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
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);
据我了解,你要的日期是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 的第三个库。
原因:
请注意,您输入了日期 17th
但它返回给您的是 16th
.. 因为它正在考虑 UTC
格式,无论何时您是 printing NSDate in NSLog
或 converting it explicitly to NSString
.
Whenever You want the date in string Then Use NSDateFormatter
as i have used in this Code..
您遇到的问题就是因为这个。
所以,无论你在哪里使用日期,只需 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]);
}
}
}
// There's no need to Increment this Date..
// "Increment" currentDate by one day.
// currentDate = [calendar dateByAddingComponents:oneDay
// toDate:currentDate
// options: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(@"Date = %@",currentDate);
}
// "Increment" currentDate by one day.
currentDate = [calendar dateByAddingComponents:oneDay
toDate:currentDate
options:0];
}
在我的代码中,我已成功获得星期六,但它是在 startdate
和 enddate
之间获得的所有星期六。
我也试过这个:
[dateComponents setWeekdayOrdinal:3]; but not working.
这是我的日志
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
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
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);
据我了解,你要的日期是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 的第三个库。
原因:
请注意,您输入了日期 17th
但它返回给您的是 16th
.. 因为它正在考虑 UTC
格式,无论何时您是 printing NSDate in NSLog
或 converting it explicitly to NSString
.
Whenever You want the date in string Then Use
NSDateFormatter
as i have used in this Code..
您遇到的问题就是因为这个。
所以,无论你在哪里使用日期,只需 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]);
}
}
}
// There's no need to Increment this Date..
// "Increment" currentDate by one day.
// currentDate = [calendar dateByAddingComponents:oneDay
// toDate:currentDate
// options:0];
希望这对您有所帮助...:)