问题
I'm trying to get the current date with :
NSLog(@"DATE NOW : %@", [NSDate date]);
But when I execute it, I have a difference of one hour between the date displayed and the current date.
Log http://data.imagup.com/12/1171458807.5637png
I found topics on questions like that but none of them gave me the answer. How can I change that ?
回答1:
You need to set the proper timezone. The default, as you can see, is UTC.
You can use a date formatter for doing that.
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone * timezone = [NSTimeZone timeZoneWithName:@"Europe/Rome"];
[dateFormatter setTimeZone:timezone];
[dateFormatter setTimeStyle:NSDateFormatterFullStyle
];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];
NSLog(@"%@", [dateFormatter stringFromDate:[NSDate date]]);
For a list of available timezones you can use
NSArray *timeZoneNames = [NSTimeZone knownTimeZoneNames];
or check out the constants here on this Rails doc page.
来源:https://stackoverflow.com/questions/14082912/nsdate-date-dont-give-me-the-right-hour