How do I format dates for different locales in EK9?
← Date, Time, and Duration · Ref: Q549
EK9's Locale type provides four named formatting levels that automatically adapt to regional conventions. No format pattern strings needed.
FOUR FORMATTING LEVELS FOR DATES
shortFormat: compact numeric (03/10/2020 GB, 10/3/20 US) mediumFormat: abbreviated month (Oct 3, 2020 US, 3 Oct 2020 GB) longFormat: full month name (3. Oktober 2020 DE) fullFormat: includes day-of-week (Samstag, 3. Oktober 2020 DE)
Each locale adjusts automatically: date order (day/month vs month/day), separators, month names, and day names.
SAME METHODS FOR TIME AND DATETIME
The four formatting levels also work on Time and DateTime:
locale.shortFormat(09:30:00) gives locale-appropriate time locale.longFormat(2024-06-15T10:30:00Z) gives full date+time+timezone
DAY OF WEEK
locale.dayOfWeek(date) returns the localized day name: Locale('de_DE').dayOfWeek(2024-06-15) gives 'Samstag' Locale('en_GB').dayOfWeek(2024-06-15) gives 'Saturday'
WHY NO PATTERN STRINGS
Pattern strings like 'yyyy-MM-dd' or 'dd/MM/yyyy' are error-prone: 'mm' means minutes in Java but months in Excel. Named levels are unambiguous and locale-correct by default.
See Q44 for the complete Locale formatting guide. See Q537 for ISO 8601 formatting. See Q551 for RFC 7231 HTTP date headers.
Example
defines module qa.locale.date.formatting defines program LocaleDateFormattingDemo() stdout <- Stdout() wedding <- 2020-10-03 lunchTime <- 12:00:01 meeting <- 2024-06-15T10:30:00Z // Create locales enGB <- Locale("en_GB") enUS <- Locale("en_US") deutsch <- Locale("de_DE") francais <- Locale("fr_FR") // Date formatting - four levels stdout.println("=== Date Formatting ===") stdout.println(`GB short: ${enGB.shortFormat(wedding)}`) stdout.println(`US short: ${enUS.shortFormat(wedding)}`) stdout.println(`GB medium: ${enGB.mediumFormat(wedding)}`) stdout.println(`US medium: ${enUS.mediumFormat(wedding)}`) stdout.println(`DE long: ${deutsch.longFormat(wedding)}`) stdout.println(`FR long: ${francais.longFormat(wedding)}`) stdout.println(`GB full: ${enGB.fullFormat(wedding)}`) stdout.println(`DE full: ${deutsch.fullFormat(wedding)}`) // Time formatting stdout.println("=== Time Formatting ===") stdout.println(`GB short time: ${enGB.shortFormat(lunchTime)}`) stdout.println(`US short time: ${enUS.shortFormat(lunchTime)}`) stdout.println(`DE medium time: ${deutsch.mediumFormat(lunchTime)}`) // DateTime formatting stdout.println("=== DateTime Formatting ===") stdout.println(`US short DT: ${enUS.shortFormat(meeting)}`) stdout.println(`GB medium DT: ${enGB.mediumFormat(meeting)}`) stdout.println(`DE long DT: ${deutsch.longFormat(meeting)}`) stdout.println(`FR full DT: ${francais.fullFormat(meeting)}`) // Day of week stdout.println("=== Day of Week ===") stdout.println(`GB: ${enGB.dayOfWeek(wedding)}`) stdout.println(`DE: ${deutsch.dayOfWeek(wedding)}`) stdout.println(`FR: ${francais.dayOfWeek(wedding)}`)
Common mistakes
E50060 — Locale has no generic format() method. Use the four named methods: shortFormat(), mediumFormat(), longFormat(), fullFormat(). See ek9 -h E50060 for details.
Incorrect:
stdout.println(`GB short: ${enGB.format(wedding, "short")}`)
Correct:
stdout.println(`GB short: ${enGB.shortFormat(wedding)}`)
Other ways to ask this
- How do I display dates in German or French format?
- How do locale formatting levels work with dates?
- How do I internationalize date display?
Coming from another language?
Java: DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).withLocale(). Python: babel.dates.format_date(style='short', locale='de_DE'). JavaScript: Intl.DateTimeFormat with options. Go: no built-in locale formatting. Rust: no built-in, chrono-locale crate. EK9: Locale('de_DE').shortFormat(date) four named levels, no pattern strings.
Keywords: time, mediumFormat, locale, regional, format, dayOfWeek, internationalization, duration, date, shortFormat, i18n, timezone, migrate, longFormat, fullFormat