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

标签 iphone ios objective-c parse-platform

我正在使用 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/

相关文章:

iphone - 从另一个类设置 NSString 属性时出错

iphone - UIPopoverController,如何改变它的大小?

iphone - iOS7 中的 CLLocation 未更新

ios - 将标签栏嵌入到 iPhone 屏幕顶部

iphone - 如何在 iphone 中制作像 pop 这样的动画而不是 push

android - Ios - 需要 Android 建议

ios - 如何处理部分瀑布布局CollectionView

iphone - iOS - setIdleTimerDisabled :NO Not Working

html - 无法删除在 flutter iOS 中使用 html/css 创建的 pdf 的页面外边距

ios - NSLayoutConstraint 将按钮放置在 View 的中心