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

Here is the code excerpt:

func mapping(map: Map) {
    time      <- (map["time"], TransformOf<Date, String>(fromJSON: {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "HH:mm:ss"
        //dateFormatter.timeZone = TimeZone(abbreviation: "EEST")
        if let argument = $0 {
            let date = dateFormatter.date(from: argument)
            return dateFormatter.date(from: argument)
        }
        return nil
        }}

$0 is string with "22:12:00". I put “let date” to see what it returns and it’s nil. I’ve looked up for format codes here: http://waracle.net/iphone-nsdateformatter-date-formatting-table/

  • app crashes after signing up and making a request - swift
  • Mutex alternatives in swift
  • How do I dispatch_sync, dispatch_async, dispatch_after, etc in Swift 3, Swift 4, and beyond?
  • How to read deeper into a snapshot Firebase Swift 3?
  • How to insert a override function into a if else statement
  • Waiting until the task finishes
  • Code should work actually. What am I doing wrong?

    EDIT: Added the whole function

    EDIT2: I just noticed it’s working properly on iPhone 7 iOS 10.1 simulator but returns nil on my iPod 10.1.1 (2016). This is so weird.

    Solutions Collect From Internet About “DateFormatter doesn't return date for “HH:mm:ss””

    From Technical Q&A QA1480 – NSDateFormatter and Internet Dates:

    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"?.