NSDateFormatter dateFromString returning wrong date

流过昼夜 提交于 2019-12-13 07:37:20

问题


Quick question.

I have a string date: example var dateString = "17-02-2015"

I use:

var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"

var newDate = dateFormatter.dateFromString(dateString)

And I get the output: 2015-01-16 23:02:00 +0000

Why am I missing a day? Is it because of the time zone? (I am at +1, and in the date the timezone is 0?).

I also tryed setting the time zone:

dateFormatter.locale = NSLocale.currentLocale()
dateFormatter.timeZone = NSTimeZone.systemTimeZone()

But no success.

What am I missing?


回答1:


Thats the UTC time and it is correct considering the fact that your LocalTime is 1+

var dateString = "17-02-2015"
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"

var newDate = dateFormatter.dateFromString(dateString)!

println(newDate.descriptionWithLocale(NSLocale.currentLocale())!)


来源:https://stackoverflow.com/questions/28557321/nsdateformatter-datefromstring-returning-wrong-date

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