ios - NSDate 将时区更改为 GMT +02.00

标签 ios timezone nsdate nsdateformatter

我有一个时区为 GMT +00.00NSDate,我想将其更改为 GMT +02.00。为此,我创建了 NSDateFormatter 并将时区设置为 UTC 但它确实给了我预期的结果,

这是我的代码,

NSDateFormatter *timeformater = [[NSDateFormatter alloc] init];
[timeformater setLocale:[NSLocale currentLocale]];
[timeformater setDateFormat:@"EEEE-dd-MMMM-yyyy HH:mm a"];
[timeformater setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];

如何正确更改时区?

最佳答案

NSDate 是一个时间点,与时区无关。您无法更改其时区,因为它没有时区。如果我们在不同的区域,同时调用 [NSDate date](例如,如果我调用你,我们同时点击一个按钮),即使我们的 watch 显示不同的时间,我们也会得到相同的 NSDate .

NSDateFormatter 用于创建字符串以根据时区显示 NSDate 值。 NSCalendar 用于与 NSDate 的计算,具体取决于时区。但是您不能更改 NSDate 的时区。

关于ios - NSDate 将时区更改为 GMT +02.00,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22448454/

相关文章:

ios - swift 3 : Caching images in a collectionView

ios - 在swift中调用具有动态类名的方法

ios - 编译器无法对表达式Swift 5进行类型检查?

JSF convertDateTime 与数据表中的时区

postgresql - Postgres : get local timestamp with time zone of latest midnight

ios - NSDateComponents 时间配置文件

ios - 使用 Swift 从 Parse 表中查询昨天的记录

IOS 如何将数组转至另一个 View Controller

django - Django 中带时区的 Unix 时间戳到日期时间

ios - DateFormatter 将日期更改了 500 年