I have CEST dates in yyyy-MM-ddTHH:mm:ssZ format. I want to show them in local format. The following function works fine. It shows the time correctly. But in the end I am forced to show the date in "dd.MM.yyyy HH:mm" format. How can i get Local/native DateFormat from iOS.
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!)
}
You can use :
dateFormatter.dateStyle = .short
dateFormatter.timeStyle = .short
But these won't come in the same format dd.MM.yyyy HH:mm
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With