Skip to content

Instantly share code, notes, and snippets.

@jimmyhoran
Last active November 8, 2018 10:24
Show Gist options
  • Save jimmyhoran/735ba987f37015f615dfe93a9508dd92 to your computer and use it in GitHub Desktop.
Save jimmyhoran/735ba987f37015f615dfe93a9508dd92 to your computer and use it in GitHub Desktop.
import Foundation
extension Date {
static func isoString(from date: Date) -> String {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(abbreviation: "GMT")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
return dateFormatter.string(from: date).appending("Z")
}
static func date(from isoString: String) -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone.autoupdatingCurrent
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
return dateFormatter.date(from: isoString)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment