Получение часового пояса (GMT) в Swift - PullRequest
0 голосов
/ 02 ноября 2018

У меня есть приложение, которое отображает часовые пояса разных стран. В настоящее время код выглядит так:

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 оно показывает GMT +0800 (фиксированное значение)

Можно ли изменить его, чтобы он отображал GMT, но без первого нуля и слова (фиксированный)?

Ответы [ 2 ]

0 голосов
/ 02 ноября 2018

Я думаю, что вы ищете этот метод

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
0 голосов
/ 02 ноября 2018

Попробуйте этот код ниже

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)
} 
...