Reputation: 9273
i can't understand why i can't change the hour of a NSDate correctly, using NSDateComponents, i'll do it in this way:
NSDateComponent components = [calendar components: (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:today];
[components setMinute:myMinute];
[components setHour:7];
NSDate *myNewDate = [calendar dateFromComponents:components];
NSLog(@"%@",myNewDate);
but the NSLog is this:
2012-12-08 06:00:00 +0000
there is one hour of difference, i have set 7 but is 6 anyone can help me?
Upvotes: 3
Views: 304
Reputation: 50089
its the timezone - the NSDate is printed with GMT time as it has no notion of timezones
use
[components setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
to make the components GMT and reflect your time set
working example
int main(int argc, char *argv[]) {
@autoreleasepool {
NSDate *today = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components: (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:today];
[components setMinute:50];
[components setHour:7];
[components setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *myNewDate = [calendar dateFromComponents:components];
NSLog(@"%@",myNewDate);
}
}
Upvotes: 6