简体   繁体   中英

NSDateFormatter dateFromString returning wrong date

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?

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())!)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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