I’m converting a string “Jun 11, 2012 9:30 PM” to an NSDate and I keep getting 4 hours ahead for some reason. The funny thing is I’m using this same string to feed a UIDatePicker in a detailed view where I have to do the same conversion, and the UIDatePicker renders the time fine. Only when I now try to NSLog it in my main and detailed view do I have problems.
This is in my views :
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMMM dd, yyyy h:mma"];
NSDate *dateFromString = [[[NSDate alloc] init]autorelease];
NSLog(@"DATE %@", _date);
dateFromString = [formatter dateFromString:_date];
NSLog(@"NSDATEFROMSTRING %@", dateFromString);
NSLog returns :
2012-06-11 00:02:09.136 LocalDeals[78090:207] DATE Jun 11, 2012 9:30 PM
2012-06-11 00:02:09.137 LocalDeals[78090:207] NSDATEFROMSTRING 2012-06-12 01:30:00 +0000
Even when I add:
[formatter setTimeZone:[NSTimeZone defaultTimeZone]];
I still get the same results from any time I choose. Any ideas? This is on the simulator by the way and yes my Region Format is set to United States.
When you NSLog an NSDate it will print the time as a GMT time zone
In order to see the correct data you will have to convert the NSDate to string using
stringFromDateThe problem you are getting is not in the NSDate but it is in Logging it
UPDATE In order to save the data to a file or database i would suggest that you save it like this
Now when you read it again from the database you would do