Thursday, October 29, 2026
HomeiOS Developmentios - Swift: Date parsing on daylight saving time

ios – Swift: Date parsing on daylight saving time

[ad_1]

I’ve acquired date-time as String from server. I’m unable to parse Date if date time is actual time of daylight saving time occur. The best way to deal with when date-time string is "2022-03-13T02:00:00.000000"

Output:

2022-03-13 05:00:00 +0000
2022-03-13 06:59:00 +0000
failed
2022-03-13 07:00:00 +0000
2022-03-13 08:00:00 +0000

Code:

extension String {
    
    func date(utilizing format: String) {
        let df = DateFormatter()
        df.dateFormat = format
        df.timeZone = .present
        if let date = df.date(from: self) {
            print(date)
        }
        else {
            print("failed")
        }
    }
}

"2022-03-13T00:00:00.000000".date(utilizing: "yyyy-MM-dd'T'HH:mm:ss.SSSSSS")
"2022-03-13T01:59:00.000000".date(utilizing: "yyyy-MM-dd'T'HH:mm:ss.SSSSSS")
"2022-03-13T02:00:00.000000".date(utilizing: "yyyy-MM-dd'T'HH:mm:ss.SSSSSS")
"2022-03-13T03:00:00.000000".date(utilizing: "yyyy-MM-dd'T'HH:mm:ss.SSSSSS")
"2022-03-13T04:00:00.000000".date(utilizing: "yyyy-MM-dd'T'HH:mm:ss.SSSSSS")

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments