Reputation: 10759
Trying to combine these two dates with NSDateComponents
"2012-09-03 00:00:00 +0000" & "1970-01-01 08:00:00 +0000"
With the following code I get --> 2012-09-02 11:00:00 +0000
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *comps = [[NSCalendar currentCalendar] components:unitFlags fromDate:session.date];
NSDate *day3 = [[NSCalendar currentCalendar] dateFromComponents:comps];
unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
comps = [[NSCalendar currentCalendar] components:unitFlags fromDate:session.start];
comps.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
day3 = [[NSCalendar currentCalendar] dateByAddingComponents:comps toDate:day3 options:0];
Upvotes: 1
Views: 736
Reputation: 10759
Found the solution.
Here is the answer:
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
cal.timeZone = [NSTimeZone timeZoneWithName:@"UTC"]; <--this is key
NSDateComponents *comps = [cal components:unitFlags fromDate:session.date];
comps.hour = 0;
comps.minute = 0;
NSDate *day3 = [cal dateFromComponents:comps];
unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *hourComps = [cal components:unitFlags fromDate:session.start];
day3 = [cal dateByAddingComponents:hourComps toDate:day3 options:0];
The result given the dates above should be 2012-09-02 08:00:00 +0000
Upvotes: 5