我不明白,这很突然,下面的代码开始提供时区缩写名称而不是时区实际名称。

let timezone = TimeZone.current.identifier


结果:GMT

期待:亚洲/加尔各答

ios - 在Swift中获取时区名称(如缩写)-LMLPHP

任何帮助表示赞赏。

最佳答案

注意:格林尼治标准时间不代表亚洲/加尔各答的时区。 (它应该给您IST)。您应该检查设备/模拟器的时区设置。如果要测试模拟器,请检查(mac)系统时区。

您的快照表明-您正在尝试使用Playground编辑器。尝试为Mac系统设置位置/时区。


  Mac系统►(转到)系统偏好设置►日期时间►时区(在这里您可以看到系统时区)


ios - 在Swift中获取时区名称(如缩写)-LMLPHP

ios - 在Swift中获取时区名称(如缩写)-LMLPHP

我尝试使用以下代码为IST / India提供给定的结果:

print("**> TimeZone.current.identifier - \(TimeZone.current.identifier)\n")
// TimeZone.current.identifier - Asia/Kolkata

print("**> TimeZone.current.description - \(TimeZone.current.description)\n")
// TimeZone.current.description - Asia/Kolkata (current)

print("**> TimeZone.current.debugDescription - \(TimeZone.current.debugDescription)\n")
// TimeZone.current.debugDescription - Asia/Kolkata (current)

09-05 17:51
查看更多