I am using NSDate formatter to take a date in from a Facebook Graph API request (which is in GMT) and convert it to the local time zone of the user. I can read in the date fine and set its new time zone fine, but when I print out the new date, it does not seem to consider daylight savings time.
time below will actually come from the request but for example here I just enter in a standard time string from the Facebook response
NSString *time = @"2012-04-12T15:25:09+0000"
NSTimeZone *localZone = [NSTimeZone localTimeZone];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:localZone];
[formatter setDateFormat:@"yyyy-mm-dd'T'HH:mm:ssZZZZ"];
NSDate *date = [formatter dateFromString:time];
[formatter setDateFormat:@"eee MMM dd, yyyy hh:mm a"];
NSString *dateStr = [formatter stringFromDate:date];
NSLog(@"%@",dateStr);
When it prints this date it prints out “Thu Jan 12, 2012 10:25 AM” which for “America/New_York” time zone, which is where I am, that is wrong. I should be “Thu Jan 12, 2012 11:25 AM” or one hour ahead. Is there a way to force day light savings time? Thanks in advance!
EDIT:
I noticed that it is actually formatting the month incorrectly. It is taking April and making it January for some reason. Why would this be?
Nope. Daylight savings time doesn’t start until March, so it would be wrong to apply it for that time.
NSDateFormatteris doing the right thing here.If you want to force a specific GMT offset, you should use
NSTimeZone timeZoneForSecondsFromGMT:instead oflocalTimeZone.