日期字符串 -> 字符串日期中断?
Date-String -> String-Date disruption?
所以,我在这里看另一个问题,然后交叉:
我有 NSDate
的扩展
extension NSDate {
private class var formatter : NSDateFormatter {
let nsDateFormatter = NSDateFormatter()
nsDateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZ"
nsDateFormatter.locale = NSLocale.currentLocale()
return nsDateFormatter
}
//NSString to NSDate
public convenience init(dateString:String) {
let dateObj = NSDate.formatter.dateFromString(dateString)!
self.init(timeInterval: 0, sinceDate: dateObj)
}
//NSDate to String
public func getString() -> String {
return NSDate.formatter.stringFromDate(self)
}
}
这很好用:
var date = NSDate() //"Feb 2, 2016, 12:36 PM"
var string = date.getString() //"2016-02-02 12:36:11 -0500"
let copyDate = NSDate(dateString: string) //"Feb 2, 2016, 12:36 PM"
copyDate.getString() //"2016-02-02 12:36:11 -0500"
But if I use, for example:
"EEE, d MMM YYYY HH:mm:ss zzz" 作为日期格式:
var date = NSDate() //"Feb 2, 2016, 12:58 PM"
var string = date.getString() //"Tue, 2 Feb 2016 12:58:48 GMT-5"
let copyDate = NSDate(dateString: string) //"Dec 22, 2015, 12:58 PM"
copyDate.getString() //"Tue, 22 Dec 2015 12:58:48 GMT-5"
为什么会这样?
它不应该像以前的情况一样工作吗?
为什么我现在有几天甚至一个月的时间中断?
您正在使用 capital Y for year instead of y。
Year (in "Week of Year" based calendars). This year designation is used in ISO year-week calendar as defined by ISO 8601, but can be used in non-Gregorian based calendar systems where week date processing is desired. May not always be the same value as calendar year.
将格式更改为 "EEE, d MMM yyyy HH:mm:ss zzz"
您可以在此处阅读有关 ISO 周日期的更多信息:https://en.wikipedia.org/wiki/ISO_week_date
所以,我在这里看另一个问题,然后交叉:
我有 NSDate
extension NSDate {
private class var formatter : NSDateFormatter {
let nsDateFormatter = NSDateFormatter()
nsDateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZ"
nsDateFormatter.locale = NSLocale.currentLocale()
return nsDateFormatter
}
//NSString to NSDate
public convenience init(dateString:String) {
let dateObj = NSDate.formatter.dateFromString(dateString)!
self.init(timeInterval: 0, sinceDate: dateObj)
}
//NSDate to String
public func getString() -> String {
return NSDate.formatter.stringFromDate(self)
}
}
这很好用:
var date = NSDate() //"Feb 2, 2016, 12:36 PM"
var string = date.getString() //"2016-02-02 12:36:11 -0500"
let copyDate = NSDate(dateString: string) //"Feb 2, 2016, 12:36 PM"
copyDate.getString() //"2016-02-02 12:36:11 -0500"
But if I use, for example:
"EEE, d MMM YYYY HH:mm:ss zzz" 作为日期格式:
var date = NSDate() //"Feb 2, 2016, 12:58 PM"
var string = date.getString() //"Tue, 2 Feb 2016 12:58:48 GMT-5"
let copyDate = NSDate(dateString: string) //"Dec 22, 2015, 12:58 PM"
copyDate.getString() //"Tue, 22 Dec 2015 12:58:48 GMT-5"
为什么会这样?
它不应该像以前的情况一样工作吗?
为什么我现在有几天甚至一个月的时间中断?
您正在使用 capital Y for year instead of y。
Year (in "Week of Year" based calendars). This year designation is used in ISO year-week calendar as defined by ISO 8601, but can be used in non-Gregorian based calendar systems where week date processing is desired. May not always be the same value as calendar year.
将格式更改为 "EEE, d MMM yyyy HH:mm:ss zzz"
您可以在此处阅读有关 ISO 周日期的更多信息:https://en.wikipedia.org/wiki/ISO_week_date