Skip to content

Instantly share code, notes, and snippets.

@christopher-fuller
Created November 4, 2016 19:16
Show Gist options
  • Save christopher-fuller/d4f6ec87f5a144e595af1de1885c5dbf to your computer and use it in GitHub Desktop.
Save christopher-fuller/d4f6ec87f5a144e595af1de1885c5dbf to your computer and use it in GitHub Desktop.
//
// Date+ISO8601.swift
// Created by Christopher Fuller for Southern California Public Radio
// Original version at https://github.com/SCPR/swift-experiments/blob/master/source/NSDateExtension.swift
// Updated by Christopher Fuller for Swift 3 compatibility
//
// Copyright (c) 2016 Southern California Public Radio
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
extension Date {
private static let stringFromDateFormatter = Date.dateFormatter(
timeZone: TimeZone(secondsFromGMT: 0)
)
private static let dateFromStringFormatter = Date.dateFormatter(
timeZone: TimeZone.autoupdatingCurrent
)
private static func dateFormatter(timeZone: TimeZone?) -> DateFormatter {
let dateFormatter = DateFormatter()
dateFormatter.timeZone = timeZone
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
return dateFormatter
}
var ISO8601String: String {
return Date.stringFromDateFormatter.string(from: self)
}
init?(ISO8601String string: String) {
guard let date = Date.dateFromStringFormatter.date(from: string) else {
return nil
}
self.init(timeInterval: 0, since: date)
}
}
@christopher-fuller
Copy link
Author

Example Usage

Convert String to Date

Supports ISO8601 formats including, but not limited to, these examples:

let _ = Date(ISO8601String: "2016-10-25T01:00:00.000Z")      // => Optional(2016-10-25 01:00:00 +0000)
let _ = Date(ISO8601String: "2016-10-25T01:00:00.000-00:00") // => Optional(2016-10-25 01:00:00 +0000)

Supports time zones:

let _ = Date(ISO8601String: "2016-10-25T01:00:00.000-07:00") // => Optional(2016-10-25 08:00:00 +0000)

Convert Date to String

Outputs GMT format:

let date = Date(timeIntervalSince1970: 1477382400)
let _ = date.ISO8601String // => "2016-10-25T08:00:00.000Z"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment