I use the following code to create an NSDate from string:
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setTimeZone:[NSTimeZone defaultTimeZone]];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSLog(@"The date from string: %@", [df dateFromString:@"2012-01-31 01:32:30"]);
What I see in the result is:
The date from string: 2012-01-31 07:32:30 +0000
This is obviously not correct.
Do you know why it adds 6 hours to my result date and how to fix it?
NSLogof dates is not a good way to display dates with time zones, instead use anNSDateFormatterto get a string for the time zone desired.NSDateis UTC based, the one true time that can be correctly displayed for any timezone withNSDateFormatter.From the Apple
NSDatedocs:Example code:
NSLog output: