dateByAddingComponents:dateRange toDate:firstDate options:0总是返回当前季度

时间:2015-04-27 09:17:00

标签: ios objective-c nsdate nscalendar nsdatecomponents

我正在使用

- (NSDate *)dateByAddingComponents:(NSDateComponents *)comps toDate:(NSDate *)date options:(NSCalendarOptions)opts

计算新日期,如下所示:

NSDateComponents *quaterComps = [NSDateComponents new];
quaterComps.quarter = 1;

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDate *nextDate = [gregorian dateByAddingComponents:quaterComps toDate:firstDate options:0];

当firstDate为2013-12-31 16:00:00 +0000时,

以上API不断返回相同的结果,而不是下一个季度

2 个答案:

答案 0 :(得分:-1)

**当前日期和下一个日期**

 NSDate *currentDate = [NSDate date];
    NSDate *futureTime = [currentDate dateByAddingTimeInterval:60*60*72];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
    [calendar setTimeZone:timeZone];
    NSDateComponents *components = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit fromDate:futureTime];
    if ([components hour] >= 19) { // make it the next day
        [components setDay:[components day] + 1 ];
    }
    [components setHour:8];
    [components setMinute:00];
    NSDate *alertTime = [calendar dateFromComponents:components];

答案 1 :(得分:-1)

test2