Created
November 4, 2016 19:16
-
-
Save christopher-fuller/d4f6ec87f5a144e595af1de1885c5dbf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example Usage
Convert String to Date
Supports ISO8601 formats including, but not limited to, these examples:
Supports time zones:
Convert Date to String
Outputs GMT format: