user2393462435
user2393462435

Reputation: 2652

NSDateFormatter returns nil for specific date only in certain time zones

Given this code:

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"yyyyMMdd"];
NSArray *datesArray = @[@"20130326", @"20130327", @"20130328", @"20130329", @"20130330", @"20130331", @"20130401"];
for (NSString *dateString in datesArray) {
    NSDate *date = [dateFormatter dateFromString:dateString];
    NSLog(@"date: %@", date);
}

This is the output in my timezone (EST):

date: 2013-03-26 04:00:00 +0000
date: 2013-03-27 04:00:00 +0000
date: 2013-03-28 04:00:00 +0000
date: 2013-03-29 04:00:00 +0000
date: 2013-03-30 04:00:00 +0000
date: 2013-03-31 04:00:00 +0000
date: 2013-04-01 04:00:00 +0000

When I go to Settings -> General -> Date & Time, and set the timezone to Amman, Jordan (GMT+3), this is the output for the same code:

date: 2013-03-25 22:00:00 +0000
date: 2013-03-26 22:00:00 +0000
date: 2013-03-27 22:00:00 +0000
date: (null)
date: 2013-03-29 21:00:00 +0000
date: 2013-03-30 21:00:00 +0000
date: 2013-03-31 21:00:00 +0000

Why?

Upvotes: 0

Views: 191

Answers (3)

meaning-matters
meaning-matters

Reputation: 23016

I ran a loop over the years, months and days from 1970 till now, at dates DST started in Amman, nil was returned.

Upvotes: 1

Douglas
Douglas

Reputation: 37781

I don't know for sure, but it is probably not a coincidence that the clocks change in Amman, Jordan on the 28th: http://www.evi.com/q/when_do_the_clocks_change_in_amman_jordan

My suspicion is that the clocks jump from 2013-03-28 21:59:59 to 2013-03-28 23:00:00, so the time 2013-03-28 22:00:00 doesn't exist in the Amman, Jordan time zone.

Upvotes: 3

KDaker
KDaker

Reputation: 5909

I think you have to specify the timezone for the dateFormatter when you set the value from string. See this question, its not exactly the same issue but it could help.

hope this helps

Upvotes: 0

Related Questions