Skip to content

Instantly share code, notes, and snippets.

View weyhan's full-sized avatar

WeyHan Ng weyhan

View GitHub Profile
@weyhan
weyhan / StaticDateFormatter.md
Created September 25, 2022 14:01
Static ISO8601DateFormatter in Swift

Static ISO8601DateFormatter in Swift

Caveat

I've never tested or verified that the solution presented here is correct or works to achieve avoiding the scenario as described in the Apple Developer Documentation.

All comments and criticism are welcome.

Background

In my recent work, I've came across a situation that I need to parse a variant of the ISO8601 date-time format. The date-time strings are from a new set of REST APIs that the app consumes in the implementation of a new feature. As the new feature needs, these date-times from the APIs needs to be in Swift Date to be useful because there will be date comparison (for sorting, searching, and etc). Additionally, for display, these dates need to be transformed to the local format or the display simply need the date part or for some other instance, need only the time part. These String date to Date conversion is either going to be at the point of the APIs are called or at the point of using them. In either case, they are happening in mul