假设是在未来,它看起来像你的fromDate
并且toDate
是倒退的。targetDate
这是我解决这个问题的方法。
创建一个类别NSCalendar
以计算特定工作日在两个日期之间出现的次数:
@interface NSCalendar (AlexCategory)
// The number of times weekday number `weekday` (1 = Sunday, 7 = Saturday)
// occurs between `fromDate` and `toDate`. If `fromDate` falls on the desired
// weekday, it is counted. If `toDate` falls on the desired weekday, it is NOT counted.
- (NSInteger)countOfWeekday:(NSInteger)weekday fromDate:(NSDate *)fromDate toDate:(NSDate *)toDate;
@end
为了实现这个新方法,我们将首先获取 的年、月、日和工作日fromDate
:
@implementation NSCalendar (AlexCategory)
- (NSInteger)countOfWeekday:(NSInteger)weekday fromDate:(NSDate *)fromDate toDate:(NSDate *)toDate {
NSDateComponents *components = [self components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSWeekdayCalendarUnit fromDate:fromDate];
fromDate
接下来,我们计算从下一个期望的工作日到多少天:
NSInteger daysUntilDesiredWeekday = weekday - components.weekday;
// If fromDate is a Wednesday and weekday is Monday (for example),
// daysUntilDesiredWeekday is negative. Fix that.
NSRange weekdayRange = [self minimumRangeOfUnit:NSWeekdayCalendarUnit];
if (daysUntilDesiredWeekday < weekdayRange.location) {
daysUntilDesiredWeekday += weekdayRange.length;
}
请注意,如果落在所需的工作日,daysUntilDesiredWeekday
它将为零。fromDate
现在我们可以NSDateComponents
在 或之后创建一个表示第一个所需的工作日fromDate
:
NSDateComponents *firstDesiredWeekday = [[NSDateComponents alloc] init];
firstDesiredWeekday.year = components.year;
firstDesiredWeekday.month = components.month;
firstDesiredWeekday.day = components.day + daysUntilDesiredWeekday;
我们更新fromDate
为第一个期望的工作日,如果它是 on 或之后返回 0 toDate
:
fromDate = [self dateFromComponents:firstDesiredWeekday];
if ([fromDate compare:toDate] != NSOrderedAscending) {
return 0;
}
fromDate
接下来我们计算从更新到的所有天数(不仅仅是所需的工作日)toDate
:
NSInteger allDaysCount = [self components:NSDayCalendarUnit
fromDate:fromDate toDate:toDate options:0].day;
我们可以将其除以一周中的天数,以仅计算所需的工作日数。由于我们从所需的工作日开始计数,任何部分周余数也将包含所需的工作日,因此我们需要四舍五入:
// Adding weekdayRange.length - 1 makes the integer division round up.
return (allDaysCount + weekdayRange.length - 1) / weekdayRange.length;
}
@end
我们可以这样测试方法:
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components.year = 2012;
components.month = 1;
components.day = 1;
NSDate *fromDate = [calendar dateFromComponents:components];
for (NSUInteger i = 1; i <= 365; ++i) {
components.day = i;
NSDate *toDate = [calendar dateFromComponents:components];
NSLog(@"%@ to %@: %ld Mondays", fromDate, toDate, [calendar countOfWeekday:2 fromDate:fromDate toDate:toDate]);
}
输出开始如下:
2012-08-06 16:27:05.751 tuesdays[81152:403] 0 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-01 06:00:00 +0000
2012-08-06 16:27:05.754 tuesdays[81152:403] 0 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-02 06:00:00 +0000
2012-08-06 16:27:05.756 tuesdays[81152:403] 1 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-03 06:00:00 +0000
2012-08-06 16:27:05.758 tuesdays[81152:403] 1 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-04 06:00:00 +0000
2012-08-06 16:27:05.759 tuesdays[81152:403] 1 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-05 06:00:00 +0000
2012-08-06 16:27:05.760 tuesdays[81152:403] 1 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-06 06:00:00 +0000
2012-08-06 16:27:05.762 tuesdays[81152:403] 1 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-07 06:00:00 +0000
2012-08-06 16:27:05.763 tuesdays[81152:403] 1 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-08 06:00:00 +0000
2012-08-06 16:27:05.763 tuesdays[81152:403] 1 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-09 06:00:00 +0000
2012-08-06 16:27:05.764 tuesdays[81152:403] 2 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-10 06:00:00 +0000
2012-08-06 16:27:05.765 tuesdays[81152:403] 2 Mondays from 2012-01-01 06:00:00 +0000 to 2012-01-11 06:00:00 +0000
根据以下输出,这看起来是正确的cal 1 2012
:
January 2012
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
(请记住,toDate
没有计算在内,因此从 2012-1-1 到 2012-1-2 有 0 个星期一,即使 2012-1-2 是星期一。)