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

查看:32
本文介绍了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 以格林威治标准时间存储日期/时间.因此默认情况下 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天全站免登陆