简体   繁体   English

dateByAddingComponents:dateRange toDate:firstDate选项:0始终返回当前季度

[英]dateByAddingComponents:dateRange toDate:firstDate options:0 is always returning current quarter

I am using 我在用

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

to calculate a new date, like below: 计算新日期,如下所示:

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

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

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

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

Above API keeps returning the same result, not the next quarter date 上面的API不断返回相同的结果,而不是下一个季度的日期

** current date and next date ** **当前日期和下一个日期**

 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];
NSDate *date = [NSDate date];
NSDateComponents *comp = [NSDateComponents new];
comp.weekOfYear = 3;
NSDate *date1 = [[NSCalendar currentCalendar] dateByAddingComponents:comp toDate:date options:0];
NSLog(@"date:  %@", date);
NSLog(@"date1: %@", date1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM