formatDate function
Formats a DateTime date according to locale conventions in pure Dart.
Supports named style presets (DateFormatStyle.short, DateFormatStyle.medium,
DateFormatStyle.long, DateFormatStyle.full) or custom pattern tokens (yyyy, yy,
MMMM, MMM, MM, M, dd, d, HH, H, hh, h, mm, ss, a).
Limitations vs Full CLDR
Includes hand-rolled month and day translations for English, French, German, and Spanish. For unsupported languages, month and weekday names fallback to English.
final date = DateTime(2026, 8, 23);
formatDate(date, locale: 'en-US'); // "8/23/2026"
formatDate(date, locale: 'en-GB'); // "23/08/2026"
formatDate(date, locale: 'de-DE'); // "23.08.2026"
formatDate(date, style: DateFormatStyle.long, locale: 'en-US'); // "August 23, 2026"
See also:
- formatDateTime, for formatting combined dates and timestamps.
- formatRelativeTime, for relative time expressions ("5 minutes ago").
- DateFormatStyle, the style enumeration.
Implementation
String formatDate(
DateTime date, {
String? locale,
String? pattern,
DateFormatStyle? style,
}) {
final loc = (locale ?? BloomI18n.instance.locale.value).replaceAll('_', '-');
final lang = loc.split('-').first.toLowerCase();
if (pattern != null) {
return _formatDateWithPattern(date, pattern, lang);
}
final effectiveStyle = style ?? DateFormatStyle.short;
return _formatDateWithStyle(date, effectiveStyle, loc, lang);
}