我有一个显示不同国家时区的应用程序。当前代码如下:
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")!)"
}
时间本身显示正确,但在aseanTimezone.text
下显示的是格林尼治标准时间+0800(固定)
是否可以更改它,使其显示GMT,但没有第一个零和单词(固定)?
答案 0 :(得分:2)
我认为您正在寻找这种方法
localizedName(for style: NSTimeZone.NameStyle, locale: Locale?)
但是,它也会删除尾随的零GMT+7
TimeZone(abbreviation: "UTC+07")!.localizedName(for: .shortStandard, locale: nil)! // => GMT+7
其他可用选项
.standard
.shortStandard
.daylightSaving
.shortDaylightSaving
.generic
.shortGeneric
答案 1 :(得分:0)
请尝试以下代码
if indexPath.row == 0 || indexPath.row == 1 || indexPath.row == 2 || indexPath.row == 6 || indexPath.row == 7 {
let newDate = Date()
formatter.dateStyle = .none
formatter.timeStyle = .short
formatter.dateFormat = "hh:mm a"
formatter.timeZone = TimeZone(identifier:"Your timezoon string here!!")
cell.aseanTimeZone.text = = formatter.string(from: newDate)
}