如何快速获取本地日期格式?

Zeb*_*dee 4 xcode timezone date swift

我有yyyy-MM-ddTHH:mm:ssZ格式的CEST 日期。我想以本地格式显示它们。以下功能工作正常。它正确显示时间。但最后我被迫以“dd.MM.yyyy HH:mm”格式显示日期。如何从 iOS 获取本地/本机 DateFormat。

func changeTime()
    {
        myLabel.text = convertTimeZoneToLocal(timeZone: "CEST", date: "2017-09-27T18:25:42Z")
    }

func convertTimeZoneToLocal(timeZone:String, date:String) -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
    dateFormatter.timeZone = TimeZone(abbreviation: timeZone)

    let dt = dateFormatter.date(from: date)
    dateFormatter.timeZone = TimeZone.current
    dateFormatter.dateFormat = "dd.MM.yyyy HH:mm"  // I want to change this line.

    return dateFormatter.string(from: dt!)
}
Run Code Online (Sandbox Code Playgroud)

Vin*_*App 8

您可以使用 :

dateFormatter.dateStyle = .short
dateFormatter.timeStyle = .short
Run Code Online (Sandbox Code Playgroud)

但这些不会以相同的格式出现 dd.MM.yyyy HH:mm