dateFromString() 返回错误的日期 [英] dateFromString() returns incorrect date

查看:63
本文介绍了dateFromString() 返回错误的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将字符串转换为日期,但结果日期不正确.

I'm trying to convert string to Date, but it result incorrect date.

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

结果

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

请告诉我我哪里出错了.我也尝试过其他格式,但它返回 nil.

Please let me know where I'm making mistake. I tried other format too, but it return nil.

推荐答案

年份格式不正确,应该是yyyy,而不是YYYY.

The format for year is incorrect, it should be yyyy, not YYYY.

Y":年(在基于一年中的一周"的日历中).这一年指定用于 ISO 8601 定义的 ISO 年-周日历,但可用于需要周日期处理的非公历日历系统.可能并不总是与日历年相同.

"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.

请参阅:日期字段符号表.
另外:ICU 格式化日期和时间

这篇关于dateFromString() 返回错误的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆