Sometimes Getting difference of an hour in NSDate

2019-09-11 14:47发布

问题:

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"]];