Skip to content

Instantly share code, notes, and snippets.

@apphands
Created May 19, 2016 06:00
Show Gist options
  • Save apphands/70c01b8edb17e64bb516842eb4d25202 to your computer and use it in GitHub Desktop.
Save apphands/70c01b8edb17e64bb516842eb4d25202 to your computer and use it in GitHub Desktop.
import Foundation
public extension NSDate {
public class func ISOStringFromDate(date: NSDate) -> String {
var dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.timeZone = NSTimeZone(abbreviation: "GMT")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
return dateFormatter.stringFromDate(date).stringByAppendingString("Z")
}
public class func dateFromISOString(string: String) -> NSDate {
var dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.timeZone = NSTimeZone.localTimeZone()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
return dateFormatter.dateFromString(string)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment