我正在使用 Parse.com 来存储一些值:
这些是 GMT 值。我如何将它们转换为设备的当前时区并获得 NSDate 作为结果?
最佳答案
NSDate 始终以 GMT 表示。这只是您表示它的方式可能会发生变化。
如果您想将日期打印到 label.text
,然后使用 NSDateFormatter
和 [NSTimeZone localTimeZone]
将其转换为字符串,如下:
NSString *gmtDateString = @"08/12/2013 21:01";
NSDateFormatter *df = [NSDateFormatter new];
[df setDateFormat:@"dd/MM/yyyy HH:mm"];
//Create the date assuming the given string is in GMT
df.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDate *date = [df dateFromString:gmtDateString];
//Create a date string in the local timezone
df.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:[NSTimeZone localTimeZone].secondsFromGMT];
NSString *localDateString = [df stringFromDate:date];
NSLog(@"date = %@", localDateString);
// My local timezone is: Europe/London (GMT+01:00) offset 3600 (Daylight)
// prints out: date = 08/12/2013 22:01
关于iphone - 将 GMT NSDate 转换为设备的当前时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18107782/