when i try log out the current date i get date different from actual date :
NSLog(@"date %@",[NSDate date]);
output 2012-04-25 23:59:28 +0000
my machine time 2012-04-26 2:02 AM
any one knows how to fix this ? thanks
EDIT
here's what i get when using nsdataformatter
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
[formatter setTimeZone:[NSTimeZone defaultTimeZone]];
NSDate *date =[formatter dateFromString:[NSString stringWithString:@"2012-04-26 01:00:00"]];
NSLog(@"date %@",date);
output : 2012-04-25 23:00:00 +0000
confusing !
There's nothing to fix. NSLog is outputting the date in GMT/UTC. If you want it in your local timezone, you need to use NSDateFormatter.
This should help you print the Local time and GMT time
// gmt
NSTimeZone *aTimeZone1 = [NSTimeZone localTimeZone];
NSInteger aTimeInterval1 = -[aTimeZone1 secondsFromGMTForDate: [NSDate date]];
NSLog(@"GMT: %@", [NSDate dateWithTimeInterval: aTimeInterval1 sinceDate: [NSDate date]]);
// local
NSTimeZone *aTimeZone2 = [NSTimeZone localTimeZone];
NSInteger aTimeInterval2 = [aTimeZone2 secondsFromGMTForDate: [NSDate date]];
NSLog(@"Local: %@", [NSDate dateWithTimeInterval: aTimeInterval2 sinceDate: [NSDate date]]);
AND
If you want to use NSDateFormatter then, how about trying this :
NSDateFormatter *aDateFormatter = [[NSDateFormatter alloc] init];
[aDateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
[aDateFormatter setDateFormat:@"dd:MM:yyyy HH:mm:ss"];
NSLog(@"GMT: %@", [aDateFormatter stringFromDate:[NSDate date]]);
[aDateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
NSLog(@"Local: %@", [aDateFormatter stringFromDate:[NSDate date]]);