I’m at the moment engaged on an iOS software and I am encountering a peculiar subject with date formatting. I’m utilizing the next Swift code to generate a timestamp in a selected format and log it to Google Analytics4:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSXXX"
let currentDate = Date()
let formattedDate = dateFormatter.string(from: currentDate) // 2024-04-20T21:23:42.22+09:00
The dateFormat I’m utilizing is meant to format the present date and time within the ISO 8601 format. Nevertheless, whereas the anticipated conduct is to get the present yr (e.g., 2024), the formatter generally returns years like 2567 or 0006.
This incorrect yr knowledge seems sporadically amongst totally different person gadgets and doesn’t correlate with any particular iOS model or gadget mannequin. The vast majority of timestamps are appropriate, however these outliers are regarding.
May anybody assist me perceive why this is likely to be occurring? Is there one thing incorrect with the best way I’m utilizing DateFormatter? Any recommendation on how to make sure the yr is all the time accurately captured could be drastically appreciated.
Thanks!