Skip to content

Instantly share code, notes, and snippets.

@dfrobison
Last active October 13, 2020 19:34
Show Gist options
  • Save dfrobison/6743baa20e9aa0336109c868c7c2c597 to your computer and use it in GitHub Desktop.
Save dfrobison/6743baa20e9aa0336109c868c7c2c597 to your computer and use it in GitHub Desktop.
[Localizing Dates] By setting the format with setLocalizedDateFormatFromTemplate(_:) instead, the formatter will do the right thing based on the locale you give it.
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US")
dateFormatter.setLocalizedDateFormatFromTemplate("MMddyyyy")
dateFormatter.string(from: Date()) // US: 09/27/2020
// ...
dateFormatter.locale = Locale(identifier: "es_BO")
dateFormatter.setLocalizedDateFormatFromTemplate("MMddyyyy")
dateFormatter.string(from: Date()) // Bolivia: 27/09/2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment