Remove "at" date format

Viewed 38

Looks like apple is defaulting the date with an at, how can I replace that at with a comma? Here's an example:

Expected result: "Mon, Oct 10, 11:22 am"

Below withLocalizedLocale is using setLocalizedDateFormatFromTemplate

let formatter = DateFormatter(withLocalizedLocale: "E, MMM d, h:mma")
formatter.amSymbol = "am"
formatter.pmSymbol = "pm"

Here's the output of the above: "Mon, Oct 10 at 11:22 am"

I tried with a space and it's the same result, however other formats such as "EEEE, MMMM dd, yyyy h:mma" works correctly, so I'm puzzle by what's going on

1 Answers

at is from your locale DateFormatter. So for your problem, you just need to change locale identifier to en_US so device not formate date to your locale one.

let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "E, MMM d, h:mm a"
formatter.locale = Locale(identifier: "en_US")
formatter.amSymbol = "am"
formatter.pmSymbol = "pm"
print("date: ", formatter.string(from: date))
Related