Open onmyway133 opened 8 years ago
public struct Formatter {
private static let basicFormatter = NSDateFormatter().then {
$0.locale = NSLocale(localeIdentifier: "en_US_POSIX")
$0.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
$0.timeZone = NSTimeZone(abbreviation: "UTC")
}
private static let formatter1 = NSDateFormatter().then {
$0.locale = NSLocale(localeIdentifier: "en_US_POSIX")
$0.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"
$0.timeZone = NSTimeZone(abbreviation: "UTC")
}
private static let formatter2 = NSDateFormatter().then {
$0.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
}
public static func date(string string: String) -> NSDate? {
return string.rangeOfString(" ") == nil
? formatter1.dateFromString(string)
: formatter2.dateFromString(string)
}
public static func string(date date: NSDate) -> String {
return basicFormatter.stringFromDate(date)
}
}
2012-09-25T07:54:41-07:00 2011-01-26T19:06:43Z 2011-01-26 19:06:43 +0000