NSDate输出不正确 [英] NSDate Output incorrectly

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

问题描述


可能重复:

从[NSDate date]关闭几个小时的日期





NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"M-d-yyyy H:mm"];

NSDate *start= [dateFormatter dateFromString:@"10-24-2012 12:15"];
NSDate *end  = [dateFormatter dateFromString:@"10-24-2012 15:30"];

当我打印出来时

   NSLog(@"------main_event start %@", start);
   NSLog(@"-----main_event end %@", end);

结果是

         ---main_event start 2012-10-24 19:15:00 +0000
         ---main_event end 2012-10-24 22:30:00 +0000

现在看起来时间自动增加7小时,12:15变为19:15,15:30变为22: 30。

Now, it looks like the time added 7 hours automatically, 12:15 becomes 19:15, and 15:30 becomes 22:30.

为什么?

推荐答案

因为时区,你的设备所在的是UTC-7。

because the timezone, where your device is located, is UTC-7.

输出为UTC(因此 +0000 ),单个NSDate将始终以UTC格式打印出它的时间。

The output is in UTC (hence the +0000), as a single NSDate will always print out it's time in UTC.

如果您使用NSDateFormatter输出日期,则会考虑您的区域设置。请在此处查看我的回答: NSDate日期方法返回错误的结果

If you use an NSDateFormatter to output the date, it will take your locale in account. See my answer here: NSDate date method returns wrong result

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

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