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

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

Leave a Comment