Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tritchey/a23dc21afca932ddaec0fdc41dd0de7d to your computer and use it in GitHub Desktop.
Save tritchey/a23dc21afca932ddaec0fdc41dd0de7d to your computer and use it in GitHub Desktop.
class ISO8601OptionalFractionalSecondsDateFormatter: ISO8601DateFormatter {
static let withoutFractionalSeconds = ISO8601DateFormatter()
func setup() {
formatOptions = [.withInternetDateTime, .withFractionalSeconds]
}
override init() {
super.init()
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
override func date(from string: String) -> Date? {
if let result = super.date(from: string) {
return result
}
return ISO8601OptionalFractionalSecondsDateFormatter.withoutFractionalSeconds.date(from: string)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment