Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i get Local DateFormat with swift?

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!)
}
like image 750
Zebedee Avatar asked Jan 04 '23 09:01

Zebedee


1 Answers

You can use :

dateFormatter.dateStyle = .short
dateFormatter.timeStyle = .short

But these won't come in the same format dd.MM.yyyy HH:mm

like image 83
Vini App Avatar answered Jan 13 '23 12:01

Vini App