9

I'm trying to convert a string to an NSDate using the following code:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *date= [dateFormatter dateFromString:@"2013-03-10T02:00:00"];

It works fine for all hours except 02:00:00, shown above, which returns nil.

Any ideas?

Rok Jarc
  • 18,765
  • 9
  • 69
  • 124
  • 4
    not sure how to fix it, but the problem could be due to daylight saving – Nabou Mar 10 '13 at 21:21
  • Yep, that's what it is. Change the date to the 9th or 11th and the problem goes away... – Richard Brown Mar 10 '13 at 21:24
  • 18
    In the USA, there is no such thing as 2am on March 10, 2013. The time went from 1:59:59 to 3:00:00 due to day light savings time. The same issue will appear in other locales as well but at other dates/times. – rmaddy Mar 10 '13 at 21:25
  • 5
    Side Note - there is no need to set the date formatter's time zone to the local timezone. That is set by default. – rmaddy Mar 10 '13 at 21:27
  • @maddy great comment! you should frame an answer. – iDroid Mar 13 '13 at 11:19

1 Answers1

1

This is because of the Daylight Savings Time that took place in the US on 10th March 2013

The System time directly jumps from 1:59 AM to 3:00 AM..
You lose an hour that you had gained on November when the DST came into effect

Mr.Anonymous
  • 820
  • 2
  • 13
  • 22