Last active
January 11, 2019 21:28
-
-
Save algal/09b08515460b7bd229fa to your computer and use it in GitHub Desktop.
NSDate+RFC3339
This file contains hidden or 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
// known-good: Xcode 7.3 (Swift 2.2) | |
import Cocoa | |
private var rfc3339formatter:NSDateFormatter = { | |
let formatter = NSDateFormatter() | |
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z" | |
formatter.timeZone = NSTimeZone(forSecondsFromGMT: 0) | |
formatter.calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierISO8601)! | |
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") | |
return formatter | |
}() | |
extension NSDate { | |
var stringFormattedAsRFC3339: String { | |
return rfc3339formatter.stringFromDate(self) | |
} | |
convenience init?(RFC3339FormattedString:String) { | |
if let d = rfc3339formatter.dateFromString(RFC3339FormattedString) { | |
self.init(timeInterval:0,sinceDate:d) | |
} | |
else { return nil } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment