在 Swift 中获取时区 (GMT)

Ham*_*Ali 2 timezone ios swift

我有一个显示不同国家时区的应用程序。目前代码如下所示:

if indexPath.row == 0 || indexPath.row == 1 || indexPath.row == 2 || indexPath.row == 6 || indexPath.row == 7{
    cell.aseanDate.text = dayFormatter.string(from: Date() as Date)
    cell.aseanTime.text = dateFormatter2.string(from: Date() as Date)
    cell.aseanTime.textAlignment = .center
    cell.aseanTimeZone.text = "\(TimeZone(abbreviation: "UTC+07")!)"
}
Run Code Online (Sandbox Code Playgroud)

时间本身显示正确,但在aseanTimezone.text其下方显示 GMT +0800(固定)

是否可以更改它以显示 GMT 但没有第一个零和单词(固定)?

Sat*_*ish 5

我想你正在寻找这种方法

localizedName(for style: NSTimeZone.NameStyle, locale: Locale?)

但是,它也会删除尾随零 GMT+7

TimeZone(abbreviation: "UTC+07")!.localizedName(for: .shortStandard, locale: nil)! // => GMT+7
Run Code Online (Sandbox Code Playgroud)

其他可用选项

.standard
.shortStandard
.daylightSaving
.shortDaylightSaving
.generic
.shortGeneric
Run Code Online (Sandbox Code Playgroud)