Reputation: 12221
I am trying to access just the hours and minutes information from a NSDate.
So I used NSCalendar to do that. Below is my code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh:mm"];
NSDate *date = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(kCFCalendarUnitHour | kCFCalendarUnitMinute) fromDate:date];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
NSLog(@"%@",date description);
NSLog(@"Hour : %d MInutes: %d",hour,minute);
It does not give me the right information. Below is the Log:
2011-10-18 18:46:12.194 DateTest[1182:707] 2011-10-18 13:16:12 +0000 2011-10-18 18:46:12.198 DateTest[1182:707] Hour : 18 MInutes: 46
Another doubt is when the time is 18:46:12.194 it is logged as 13:16:12. Why is this difference?
Upvotes: 0
Views: 243
Reputation: 243156
Because you live in a time zone that is 5.5 hours ahead of GMT, and NSDate
's -description
method always returns a string formatted in the GM time zone.
Upvotes: 2