Thanks, this worked for me !
2011-11-16 12:14:08.806 Learning1[620:707] The Date is: 2011-11-16 17:14:08 +0000
2011-11-16 12:14:08.808 Learning1[620:707] The time is: 2011-11-16 - 12:14:08
[quote=“nxspam”]Hi Guys, got it!
You can not set a timezone for an NSDate object.
After a lot of digging, this strange behavior is by design. NSDate records a single (universal?) point in time that is set to GMT+0
When you display it, you need an NSDateFormatter and then you can then set a timezone on that to correctly display the recorded time in your timezone.
// this does not work correctly, it defaults to GMT and I am in GMT+1
NSDate *wrongDate = [[NSDate alloc]init];
NSLog(@"This date object says: %@",wrongDate); //Prints GMT (1 hour behind).
NSDateFormatter *df = [[NSDateFormatter alloc] init ];
// Set a TimeZone for the DateFormatter
[df setTimeZone:[NSTimeZone localTimeZone]];
[df setDateFormat:@"yyyy-MM-dd - hh:mm:ss"];
NSLog(@"The formatter says: %@", [df stringFromDate:wrongDate]); //now shows correct date/time
This is pretty goofy behaviour and produces dates and times that need a lot of work. For example, if we save the value of NSDate to a file or a database, we’re still saving the time in GMT+0 and not a local time!
I wonder why then, it works for Aaron and others in the US, because GMT+0 is London, England ?