NSCalendar 和 NSDate 不兼容
我正在尝试从 NSDate 访问小时和分钟信息。
所以我使用 NSCalendar 来做到这一点。下面是我的代码:
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);
它没有给我正确的信息。以下是日志:
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] 小时:18 分钟:46
另一个疑问是,当时间为 18:46:12.194 时,它会记录为 13:16:12。为什么会有这样的差异呢?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.