Search code examples
swiftnsdateformatterdateformatter

How to remove 'at' from formatted date string?


This is the date formatter setup:

let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .medium

Expected output:

Aug 13, 2017 6:04:11 PM

Current output:

Aug 13, 2017 at 6:04:11 PM

How can the 'at' word be removed ideally without specifying a format string and where does it come from?

With Austrian German locale:

dateFormatter.locale = Locale(identifier: "de_AT")
13.08.2017, 18:04:11

With US English locale:

dateFormatter.locale = Locale(identifier: "en_US")
Aug 13, 2017 at 6:04:11 PM

Solution

  • You can achieve the expected output using

    dateFormatter.dateFormat = "MMM dd, yyyy h:mm:ss a".

    The at word is put there due to the current locale, so if you want to get rid of it for all locales, you should specify the exact date format as you can see above.