为什么 NSDate 报告错误的日期?
这是我的代码。
NSDate *today = [NSDate date];
NSString *todayString = [[today description] substringToIndex:10];
NSLog(@"Today: %@", todayString);
我得到的是 2011-07-19 而不是 2011-07-18。关于可能出现的问题有什么想法吗?
Here is my code.
NSDate *today = [NSDate date];
NSString *todayString = [[today description] substringToIndex:10];
NSLog(@"Today: %@", todayString);
I am getting 2011-07-19 instead of 2011-07-18. Any ideas on what may be the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
NSDate 的
description
方法返回 UTC 时间,如果您在夏令时期间位于美国东部时区,则该时间比您的挂钟时间晚 4 小时。换句话说,按 UTC 时间计算,晚上 10 点您的时间就是第二天凌晨 2 点。修复它的通常方法是使用 NSDateFormatter 相反,并在必要时显式设置时区。
NSDate's
description
method returns times in UTC, which if you are in the US Eastern Time Zone during daylight savings time is 4 hours later than your wall clock time. In other words, at 10pm your time it is 2am the next day in UTC.The usual way to fix it is to use NSDateFormatter instead, and explicitly set the time zone if necessary.