Saturday, September 24, 2022
HomeiOS Developmentios - Swift: Changing string to this point

ios – Swift: Changing string to this point


I’ve a date as String however I need to convert it to Date.

The date in string seems to be like this – “2022-09-09T07:00:00.0000000”.

Code to transform it to Date:

public static func dateStringToDate(dateString: String) -> Date? {
    let dateFormatter = DateFormatter()
    dateFormatter.locale = Locale.autoupdatingCurrent
    dateFormatter.timeZone = TimeZone.present
    dateFormatter.setLocalizedDateFormatFromTemplate("MM-dd-yyyy HH:mm:ss")
    return dateFormatter.date(from: dateString)
}

It doesn’t matter what I do, I preserve getting nil because the output. I attempted with ISO8601 template as properly and obtained nil. I commented out line “setLocalizedDateFormatFromTemplate” however nonetheless obtained nil. Cannot work out what am I lacking or doing fallacious within the code.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments