A date formatter based on ISO8601DateFormatter that can parse all ISO dates. This includes:
2022-11-11
),2022-11-11T11:30:00Z
),2022-11-11T11:30:00.344Z
).The ISO 8601 standard defines a representation of dates and times as an international standard to exchange date and time data in an unambigous way. This standard is widely used to exchange dates with and without times between APIs and client applications, such as iOS apps.
Apple provides an implementation, as part of Foundation, to parse and write ISO 8601 dates and times called ISO8601DateFormatter. This implementation is not very flexible: it requires a set of options to be passed that defines how the date is written and doesn't support parsing multiple formats at the same time.
This subclass fixes this problem by making changes to the formatOptions
property on the fly before parsing the input string.
Starting with version 2, SmartISODateFormatter is available through Swift Package Manager.
.package(url: "https://github.com/MrAsterisco/SmartISODateFormatter", from: "<see GitHub releases>")
To find out the latest version, look at the Releases tab of this repository.
SmartISODateFormatter is a subclass of ISO8601DateFormatter
, so you can use it the same way as the original class.
let string = "2022-11-11T11:30:00.344Z"
let date = SmartISODateFormatter().date(from: string)
SmartISODateFormatter also comes bundled with a custom dateDecodingStrategy
for JSONDecoder
. You can set it as follows:
let jsonDecoder = JSONDecoder()
jsonDecoder.dateDecodingStrategy = .smartISO8601
This will empower the JSONDecoder
to parse all the formats that are supported by SmartISODateFormatter.
You are very welcome to fork this repository, make changes and create a pull request.
SmartISODateFormatter is released under the MIT license. See LICENSE for more information.
link |
Stars: 1 |
Last commit: 44 weeks ago |
This version converts the library to a Swift Package. The library is no longer available on CocoaPods. Please, migrate to Swift Package Manager.
New Features
formatOptions
.smartISO8601
dateDecodingStrategy
available for JSONDecoder
.Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics