I have a date.
NSLog
shows it as:
2014-05-11 21:59:59 +0000
Now I want to get the same date but with hour, minutes and seconds set to zero:
2014-05-11 00:00:00 +0000
My code:
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
NSDateComponents *components = [calendar components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:dayOfWeek];
NSLog(@"components: %@", components);
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:[components year]];
[comps setMonth:[components month]];
[comps setDay:[components day]];
[comps setHour:0];
[comps setMinute:0];
[comps setSecond:0];
NSLog(@"New date: %@", [calendar dateFromComponents:comps]);
But NSLog
shows:
2014-05-10 22:00:00 +0000
Seems the day and hour is calculated wrong.
Note, that I am in Germany, but I use [NSTimeZone localTimeZone]
because I assume this should return the correct timezone?