DateFormatter doesn't return date for “HH:mm:ss”

后端 未结 1 1881
无人共我
无人共我 2020-11-21 23:50

Here is the code excerpt:

func mapping(map: Map) {
    time      <- (map[\"time\"], TransformOf(fromJSON: {
        let dateFormatter          


        
相关标签:
1条回答
  • 2020-11-22 00:42

    From Technical Q&A QA1480 – NSDateFormatter and Internet Dates (emphasis added):

    On the other hand, if you're working with fixed-format dates, you should first set the locale of the date formatter to something appropriate for your fixed format. In most cases the best locale to choose is "en_US_POSIX", a locale that's specifically designed to yield US English results regardless of both user and system preferences.

    This will prevent the date from being interpreted according to the user's regional settings:

    let dateFormatter = DateFormatter()
    // Set the locale first ...
    dateFormatter.locale = Locale(identifier: "en_US_POSIX")
    // ... and then the date format:
    dateFormatter.dateFormat = "HH:mm:ss"
    
    // ...
    

    See also What is the best way to deal with the NSDateFormatter locale "feechur"?.

    0 讨论(0)
提交回复
热议问题