gpt4 book ai didi

iphone - 将 GMT NSDate 转换为设备的当前时区

转载 作者:技术小花猫 更新时间:2023-10-29 10:18:26 26 4
gpt4 key购买 nike

我正在使用 Parse.com 来存储一些值:

enter image description here

这些是 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com