简体   繁体   中英

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,

Above API keeps returning the same result, not the next quarter date

** 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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