简体   繁体   English

dateFromString()返回错误的日期

[英]dateFromString() returns incorrect date

I'm trying to convert string to Date, but it result incorrect date. 我正在尝试将字符串转换为Date,但是它导致日期不正确。

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd MMM YYYY"
let dt = dateFormatter.dateFromString("17 Sep 2015")
println("Date : \(dt)") 

It result 结果

Date : Optional(2014-12-20 18:30:00 +0000)

Please let me know where I'm making mistake. 请让我知道我在哪里犯错。 I tried other format too, but it return nil. 我也尝试了其他格式,但返回nil。

The format for year is incorrect, it should be yyyy , not YYYY . 年的格式不正确,应为yyyy ,而不是YYYY

"Y": Year (in "Week of Year" based calendars). “ Y”:年(在“周”日历中)。 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. 今年的名称用于ISO 8601定义的ISO年-周日历中,但也可用于需要周日期处理的基于非格里高里历的日历系统中。 May not always be the same value as calendar year . 值不一定总是与日历年相同

See: Date Field SymbolTable. 请参阅: 日期字段SymbolTable。
Also: ICU Formatting Dates and Times 另外: ICU格式化日期和时间

Your date format string is wrong. 您的日期格式字符串错误。 Change it to the following: 将其更改为以下内容:

dateFormatter.dateFormat = "dd MMM yyyy"

For more information read Date Formatters documentation. 有关更多信息,请阅读日期格式化程序文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM