简体   繁体   中英

NSDateFormatter dateFormat ShortStyle not working

I have some problem with the NSDateFormatter now.

When I set the dateStyle to be ShortStyle , the code will work if I set the language of the simulator into Simplified Chinese. If I set the date is 2015-04-20 , it will show 15/4/20 . According to the documentation , this result is correct. And since I set the locale of the dateFormatter , the date will be shown in a year/month/date format, which meets the date writing habit of Chinese.

ShortStyle

Specifies a short style, typically numeric only, such as “11/23/37” or “3:30 PM”.

If I set the language of the simulator into Japanese, and the same code will lead to the result of 2015/04/20 . This is different with the description of the documentation, but it is working anyway.

However, if I set the language of the simulator into English, the same code will cause an error. The code is showing as the following, and the error says "fatal error: unexpectedly found nil while unwrapping an Optional value" . It points to the line of let tmpStartDate: NSDate = dateFormatter.dateFromString(self.room!.startdate)!

var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let currentTimeZone: NSTimeZone = NSTimeZone.localTimeZone()
dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle
dateFormatter.timeZone = currentTimeZone
let locale = NSLocale.currentLocale()
dateFormatter.locale = locale
let tmpStartDate: NSDate = dateFormatter.dateFromString(self.room!.startdate)!
let start: String = dateFormatter.stringFromDate(tmpStartDate)

But this is strange. The same code and the same date will run if the simulator's language is Simplified Chinese or Japanese (though a little bit different). But it will lead to a fatal error in English.

And I have to say that self.room.startdate is not nil. It is got from server, and will never be nil in any situation. I can even print it out.

Also, the similar code which uses ShortStyle in another view can work correctly . I have no idea why this happens.

Does anyone know why this problem happen? And how to solve it if I really want to use the NSDateFormatterStyle.ShortStyle , rather than set the dateFormat directly into 'yy/MM/dd' ?

My friend looked at the code and point out the problem.

This problem is caused by the differences of the date format between Chinese/Japanese and English. In Chinese/Japanese, the date format is year/month/date . But in English, the date format is month/date/year .

In my code, I set the dateStyle into ShortStyle before I transfer the date from string. It will work correctly since the start date string is also in a year-month-date format. But in English, the ShortStyle is month/date/year . But the date format of the start date string is still year-month-date . And this will lead to the failure of transferring date from string, and cause the application crashes.

To solve this, just move the line dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle after the let tmpStartDate: NSDate = dateFormatter.dateFromString(self.room!.startdate)! line.

Then the code will work correctly, and show different date format for different region. ( Year/month/date for Chinese/Japanese, and month/date/year for English.)

Special thanks to my friend SorziaX, though he does not have a stackoverflow account.

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