malcommac / SwiftDate

🐔 Toolkit to parse, validate, manipulate, compare and display dates, time & timezones in Swift.
MIT License
7.61k stars 767 forks source link

Date parser fails when device is in 12-Hour Time mode #796

Open matejhacin opened 2 years ago

matejhacin commented 2 years ago

Hello, I am parsing dates in my app using this library and I've just noticed an issue when my device is in 12-Hour Time mode.

The API I am working with can return dates in two different formats:

Format A: 2021-12-06T00:00:00 Format B: 2020-06-10T06:00:00.785+02:00

Parsing Format B always works whether device is in 12-Hour or 24-Hour mode.

Parsing Format A however, will fail if the device is in 12-Hour mode. It does work in 24-Hour mode.

I am currently using a temporary workaround, by providing my own formatter and Region when using the toDate() extension. Like this:

"2020-06-10T06:00:00".toDate() // Fails in 12-Hour Time mode "2020-06-10T06:00:00".toDate("yyyy-MM-dd'T'hh:mm:ss", region: Region.UTC) // Works both in 12-Hour and 24-Hour mode

wonder2011 commented 2 years ago

@matejhacin Try parsing dateA with .toDate("yyyy-MM-dd'T'hh:mm:ss", region: Region.ISO) !

marcbaldwin commented 8 months ago

I faced this problem but was able to fix it by setting the Locale of the Region to "en_US_POSIX". You can read more about why this works here