سؤال

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?

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top