NSDateFormatter wrong date

我是研究僧i 提交于 2019-12-14 04:17:17

问题


As simple as

     NSDateFormatter *df  = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"dd/MM/yyyy"];
     NSLog(@"Current date in event %@ %@",currentDate,[df stringFromDate:currentDate]);

But in the log

    Current date in event 2013-10-01 22:00:00 +0000 02/10/2013

Why?


回答1:


When you log an NSDate it always prints in UTC time. When you use stringFromDate: you get a date in local time unless you change the timezone of the date formatter.

In this case you are in a timezone two hours ahead of UTC (assuming you are in Italy).

Since the NSDate object is October 1, 2013 at 22:00 UTC, this is the same as midnight of October 2, 2013 at UTC+2. This is why you see the string showing October 2.

Here's another way to look at it. You are not seeing a 1 day difference. You are actually seeing a 2 hour difference due to your timezone. From midnight to 0200 hours your time, you will see what appears to be a 1 day difference.



来源:https://stackoverflow.com/questions/19122239/nsdateformatter-wrong-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!