%y-%m-%dt%h:%m:%s.%fz python to swift isoformatter in swift

You can use ISO8601DateFormatter class in Swift to convert a string in %y-%m-%dt%h:%m:%s.%fz format to date object.

Here's an example code snippet:

main.swift
let dateString = "2022-01-01T12:34:56.789Z"

let dateFormatter = ISO8601DateFormatter()
dateFormatter.formatOptions = [.withFractionalSeconds, .withDashSeparatorInDate, .withColonSeparatorInTime, .withTimeZone]

if let date = dateFormatter.date(from: dateString) {
    print(date)
} else {
    print("Invalid date string")
}
325 chars
11 lines

This will output the date object in ISO format.

You can customize the format options based on your requirement.

Note: In Python %y is Year without century as a zero-padded decimal number and %Y is Year with century as a decimal number. So, you may need to adjust the format string accordingly.

related categories

gistlibby LogSnag