Sometimes Getting difference of an hour in NSDate

守給你的承諾、 提交于 2019-12-11 18:28:43

问题


I am using IOS 6 and IOS 5. I am getting difference in time of an hour. while I have used the date format as @"MMM d, yyyy HH:mm aaa". I have also tried @"MMM d, yyyy h:mm aaa".

Actually, I am setting fixed time of notifications for current local. The time is fixed as 10:00AM for different dates. It is displaying date correctly in console but in time is varying as for Australia Time Zone it is displaying "2012-10-02 14:00:00 +0000 or 2012-10-02 13:00:00 +0000".

When I have change the date format as @"MMM d, yyyy h:mm aaa" it is displaying "2012-10-02 00:00:00 +0000 or 2012-10-02 23:00:00 +0000".

Please Suggest.


回答1:


When using a date formatter, you should specify the calendar and time zone to return dates in the correct format.

I've found that adding the following two lines has helped alleviate this issue in the past. (You see what I did there?)

[formatter setCalendar:[NSCalendar currentCalendar]];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];


来源:https://stackoverflow.com/questions/12689524/sometimes-getting-difference-of-an-hour-in-nsdate

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