Reputation: 25
I live in India which is +5.30 hours GMT. When I print the system time, it shows me the current time. But when I use this code, the GMT time is used!
Code example:
NSDate *now = [NSDate date];
NSDate *tomorrow = [now dateByAddingTimeInterval:24 * 60 * 60];
NSDate *dayaftertomorrow = [now dateByAddingTimeInterval:48 * 60 * 60];
NSLog(@"Tomorrow, the date will be %@" , tomorrow);
NSLog(@"Day after tomorrow, the date will be %@" , dayaftertomorrow);
The system time now is 11:34 P.M. but the console prints out :
2013-06-05 23:35:02.577 prog1[1601:303] Tomorrow, the date will be 2013-06-06 18:05:02 +0000 2013-06-05 23:35:02.578 prog1[1601:303] Day after tomorrow, the date will be 2013-06-07 18:05:02 +0000
Upvotes: 0
Views: 1500
Reputation: 730
You can do this with a NSDateFormatter
:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
NSLog(@"Now: %@", [dateFormatter stringFromDate:[NSDate date]]);
Other timezones can be found with NSTimeZone
like this:
NSLog(@"Timezones: %@", [NSTimeZone knownTimeZoneNames]);
Hope that helps!
Upvotes: 2
Reputation: 1806
After you create you NSDate object, you have to use the NSDateFormatter method stringFromDate: to generate a date string localized to a particular timezone. If you just do a straight NSLog() on the NSDate object it will show the date as GMT by default
Upvotes: 4