Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
NSDate+RFC3339
// 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
You can’t perform that action at this time.