Friday, November 6, 2026
HomeiOS Developmentios - Swift 5: DateFormatter Convert String to Date with Extension

ios – Swift 5: DateFormatter Convert String to Date with Extension

[ad_1]

I wish to convert a string to a date, after which return a string once more.
I did this extension, however after I use it it would not work because it ought to.

For instance if I’ve “2020-12-10” the consequence must be “10 de Diciembre de 2020”, nonetheless I at all times get the identical consequence “22 de Diciembre de 2021” and its not appropriate.

That is my code:

extension String {
    func dateFormatter(fashion: DateFormatter.Model) -> String? {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        formatter.timeZone = .present
        formatter.dateStyle = fashion
        formatter.locale = Locale(identifier: "es_AR")
        return formatter.string(from: Date())
    }
}

let dateExample = "2022-09-08" // This must be "8 de Septiembre de 2022"

let dateFormat = dateExample.dateFormatter(fashion: .lengthy) // right here i get "22 de diciembre de 2021"

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments