0

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

Chirag Shah
  • 3,034
  • 1
  • 30
  • 61
Wingzero
  • 9,644
  • 10
  • 39
  • 80
  • please go with this link http://stackoverflow.com/questions/11699706/ios-and-objective-c-repeating-an-event-every-quarter – Chirag Shah Apr 27 '15 at 09:30

2 Answers2

-1

** 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];
Vijay yadav
  • 1,220
  • 8
  • 17
  • 1
    could you explain what your code does? I want get a date with its quarter + 1, but I don't see your code can do that. – Wingzero Apr 27 '15 at 09:35
-1
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)
Vijay yadav
  • 1,220
  • 8
  • 17
  • 1
    Stop flooding answers... I know weekOfYear, but what I am asking is the quarter behaviour. I got strange behaviour for quarter, but others are good – Wingzero Apr 27 '15 at 10:12