I found out some strange behaviour of DateFormatter on iOS 13.4.1 while 12 hour date style set in the iOS settings and now is PM.
class DateFormatterTest {
static var dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS Z"
return formatter
}()
static let testDate = Date()
static func printDateString() {
print(dateFormatter.string(from: testDate))
}
}
DateFormatterTest.printDateString()
// prints 2020-08-01 15:32:58.765 +0300 when 24h date style set in the iOS 13.4.1 system
// prints 2020-08-01 3:32:58.765 +0300 when 12h date style set in the iOS 13.4.1 system (EXPECTED: same as when 24h date style set.)
Note that while testing this using Playground there is no matter which date style set on MacOS the playground always prints "2020-08-01 15:32:58.765 +0300" which is correct.
The question is how to get correct formatted date string in format "yyyy-MM-dd HH:mm:ss.SSS Z" while 12h time style set on iOS 13.4.1
I didn't test on other iOS versions, so please comment if you see (or don't see) the same behaviour on another iOS version
The problem is fixed. Locale needs to be set.
formatter.locale = Locale(identifier: "en_US_POSIX")