NSDate格式输出错误的日期 [英] NSDate Format outputting wrong date

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

问题描述

我有一个NSString(例如2011-04-12 19:23:39),我将其格式化为NSDate的做法如下:

I have a NSString (ex. "2011-04-12 19:23:39"), and what I did to format it to a NSDate was the following:

[inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSDate *date = [inputFormatter dateFromString:newDateString];

但是当我记录日期时输出的是:

but what it outputs when I nslog the date is this:

2011-04-12 23:23:39 +0000

2011-04-12 23:23:39 +0000

约4小时休息。我错过了什么吗?可能是时区问题?

which is about 4 hours off. Is there something I missed? Possibly a time zone problem?

推荐答案

NSDateFormatter 在创建NSDate对象时使用当前设备时区。 NSDate以GMT格式存储日期/时间。因此,默认情况下,NSLog将以GMT + 0输出日期/时间。所以,你的代码没有任何问题。现在,如果要将NSDate输出到当前时区,则必须使用NSDateFormatter对象。

NSDateFormatter use the current device timezone when it created the NSDate object. NSDate stores the date/time in GMT. Therefore by default NSLog will output the date/time in GMT+0. So, there's nothing wrong with your code. Now if you want to output the NSDate to your current timezone, your will have to use a NSDateFormatter object.

这篇关于NSDate格式输出错误的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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