Swift: can't get NSDate dateFormatter right

[亡魂溺海] 提交于 2019-12-18 09:46:42

问题


DateFormatter is acting funny to me:

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM'/'DD'/'YYYY"
var dateString = "09/28/1989"
var date = dateFormatter.dateFromString(dateString)
println(date)

Returns:

"Optional(1988-12-24 13:00:00 +0000)"

Any kind of help will be appreciated.


回答1:


It looks like your date formatter is a bit off. "MM'/'DD'/'YYYY" reads "Padded month / day of the year (not month) / year of the current week (so Jan 1-6 could overlap and add side-effects)"

I'm guessing that what you're aiming for is "MM'/'dd'/'yyyy", which reads "Padded month / padded day of month / year"

For reference, here's the currently-used Unicode Technical Standard that Apple documents as their standard for iOS 7+ and OSX 10.9+ here



来源:https://stackoverflow.com/questions/28404594/swift-cant-get-nsdate-dateformatter-right

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!