UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Date formatting seems to be adding one day.

Forums > Swift

Hello, I have a strange problem with dates.

In the code below yyyy-MM-dd'T'HH:mm:ss.SSSSZ = 2023-01-07T23:01:00.341533Z But the formated date analysisProjects.apDateLaunchedUnformatted[apCrawlNo] prints out as 20230108 i.e. one day later.

Any idea what i'm doing wrong here?


        // Generate the unformated short date launched
        dateFormatterToDate.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSZ"
        if let date = dateFormatterToDate.date(from: isoDate) {
            let dateFormatterToString = DateFormatter()
            dateFormatterToString.dateFormat = "YYYYMMDD"
            analysisProjects.apDateLaunchedUnformatted[apCrawlNo] = dateFormatterToString .string(from: date)
        }

2      

2      

Dates are stored as UTC. Formatted dates are localized. Check that 2023-01-07T23:01:00.341533Z in UTC isn't actually 2023-01-08 in your local time zone. If you don't want the formatted string to be localized, you will need to supply a fixed locale, like en_US_POSIX.

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.