I'm trying to convert the current time and date to a timestamp format. Since the service is receiving this format for timestamps:
2018-26-11T11:38:00Z
I decided to use a format like this:
yyyy-M-dd'T'H:mm:ss'Z'
But, when I use a date formatter to convert it, I'm getting an unwanted AM/PM tag at the end by default:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-M-dd'T'H:mm:ss'Z'"
let currentTimeAndDate = Date()
let timeStamp = dateFormatter.string(from: currentTimeAndDate)
// Prints "2018-12-05T12:58:38 PMZ"
How can I remove that AM/PM by default at the end of the string?
Set the locale to fixed en_US_POSIX"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
Or use the dedicated ISO8601 formatter
let dateFormatter = ISO8601DateFormatter()
let currentTimeAndDate = Date()
let timeStamp = dateFormatter.string(from: currentTimeAndDate)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With