为什么realDate
结果为零值?为什么此字符串格式导致无NSDate?
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let testString = "2015-07-01 05:00:00 +0000"
let realDate = dateFormatter.dateFromString(testString)
为什么realDate
结果为零值?为什么此字符串格式导致无NSDate?
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let testString = "2015-07-01 05:00:00 +0000"
let realDate = dateFormatter.dateFromString(testString)
因为它与日期格式不匹配。对于realDate
你有这将是未来的格式:
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
hh是12小时格式 –
testString实际上来自真正的日期有没有办法忽略时间组件,所以我只有日期? – 4thSpace
@ 4thSpace你可以这样做'let testString = dateFormatter.stringFromDate(NSDate())'格式为:'dateFormatter.dateFormat =“yyyy-MM-dd”'如果我明白你想要做什么,这会给字符串:'2016-02-26' –
通只有你定义在你的日期格式元素“2015年7月1日 –
或者将其定义为” YYYY-MM-DD HH:MM:SS X “ –