为什么NSDate报错日期? [英] Why NSDate is reporting the wrong date?

查看:99
本文介绍了为什么NSDate报错日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码。

NSDate *today = [NSDate date];        
NSString *todayString = [[today description] substringToIndex:10];
NSLog(@"Today: %@", todayString);

我得到2011-07-19而不是2011-07-18。关于可能出现什么问题的任何想法?

I am getting 2011-07-19 instead of 2011-07-18. Any ideas on what may be the problem?

推荐答案

NSDate的描述方法返回UTC时间,如果您在夏令时期间在美国东部时区,则比您的挂钟时间晚4小时。换句话说,在晚上10点,你的时间是UTC的第二天凌晨2点。

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.

解决它的常用方法是使用 NSDateFormatter ,并在必要时明确设置时区。

The usual way to fix it is to use NSDateFormatter instead, and explicitly set the time zone if necessary.

这篇关于为什么NSDate报错日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆