计算iOS中的日历日

时间:2014-03-07 11:58:17

标签: ios objective-c nsdate nscalendar nsdatecomponents

我有一项任务是计算在某个截止日期到期之前还剩多少天。 例如:我从今天开始有10个工作日的截止日期(让我们假设它是星期一)。我需要计算截止日期到期之前剩余的日历天数。这意味着我需要查看这个时间段内有多少个周末并将它们添加到“剩余天数”结果中(在我们的具体示例中应该是12天,因为在这个时间段内只有一个周末将会下降)。

我如何在iOS上执行此操作?

如果您需要更多说明,请告诉我

3 个答案:

答案 0 :(得分:0)

在这里做一些计算。 并尝试这样。

NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents     = [gregorian components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today];

//calcuate number of days to substract from today
NSDateComponents *componentsToSubtract  = [[NSDateComponents alloc] init];
[componentsToSubtract setDay: (0 - ((dayofweek + 5) % 7))];
[componentsToSubtract setHour: 0 - [weekdayComponents hour]];
[componentsToSubtract setMinute: 0 - [weekdayComponents minute]];
[componentsToSubtract setSecond: 0 - [weekdayComponents second]];

//Create date for first day in week
NSDate *beginningOfWeek = [gregorian dateByAddingComponents:componentsToSubtract toDate:today options:0];


NSDateComponents *componentsToAdd = [gregorian components:NSDayCalendarUnit fromDate:beginningOfWeek];
[componentsToAdd setDay:6];

NSDate *endOfWeek = [gregorian dateByAddingComponents:componentsToAdd toDate:beginningOfWeek options:0]

答案 1 :(得分:0)

int numberOfDays=30;

NSDate *startDate=[NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *offset = [[NSDateComponents alloc] init];
NSMutableArray *dates = [NSMutableArray arrayWithObject:startDate];
NSMutableArray *weekends = [NSMutableArray new];
NSMutableArray *businessDays = [NSMutableArray new];

if (![self isDateWeekend:startDate]) {
    [businessDays addObject:startDate];
}

for (int i = 1; i < numberOfDays; i++) {
    [offset setDay:i];
    NSDate *nextDay = [calendar dateByAddingComponents:offset toDate:startDate options:0];
    [dates addObject:nextDay];
    [businessDays addObject:nextDay];
    if ([self isDateWeekend:nextDay]) {
        [weekends addObject:nextDay];
        [businessDays removeObject:nextDay];
    }
}

NSLog(@"All Days%@",dates);
NSLog(@"All Weekends%@",weekends);
NSLog(@"Business Days%@",businessDays);

周末功能是从其他SO答案借来的,因为它更整洁,

- (BOOL) isDateWeekend:(NSDate*)date
{
    NSRange weekdayRange = [[NSCalendar currentCalendar] maximumRangeOfUnit:NSCalendarUnitWeekday];
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:date];
    NSUInteger weekdayOfDate = [components weekday];
    if (weekdayOfDate == weekdayRange.location || weekdayOfDate == weekdayRange.length) {
            //the date falls somewhere on the first or last days of the week
            return YES;
        }
        else
            return NO;
 }

这可以扩展到日期范围内的所有日期,周末和工作日。

答案 2 :(得分:0)

试试这个,

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear| NSWeekdayCalendarUnit fromDate:[NSDate date]];
int weekDay = components.weekday;
int numberOfweeks = numberOfDays/5;
int weekDayDiff = 7 - weekDay;
if (weekDayDiff >= 5 && numberOfDays%5 == 0) {
    numberOfweeks --;
}
numberOfDays += numberOfweeks * 2;