ios – Swift: Changing string thus far

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

The date in string appears to be like 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 dateString)

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

Leave a Reply