skip to content

In Angular Material 22, what do DateAdapter, MAT_DATE_FORMATS and MAT_DATE_LOCALE each decide, and how do you set them for German dates held as Luxon values?

level: middleimportance: must knowfreq 40%

answer

  1. three providers, three separate jobs
  2. the adapter fixes the value type
  3. format values speak the adapter's dialect
  4. locale defaults from the app locale
  5. Luxon tokens, a locale code string

basics

~20 s

DateAdapter decides the date type and how dates are parsed, formatted and compared; MAT_DATE_FORMATS holds the parse and display formats that adapter understands; MAT_DATE_LOCALE sets its locale. For Luxon: provideLuxonDateAdapter(germanFormats) plus MAT_DATE_LOCALE set to 'de'.

solid answer

~40 s

Material's datepicker and timepicker never touch a date directly: everything goes through an injected `DateAdapter<D>`, so the adapter decides the value type (`Date` for the native and date-fns adapters, Luxon `DateTime`, `Moment`) and how text is parsed and displayed. `MAT_DATE_FORMATS` is a `MatDateFormats` object (`parse.dateInput`, `display.dateInput`, `monthYearLabel`, `dateA11yLabel`, `monthYearA11yLabel`, plus optional time fields) whose values Material passes to the adapter untouched, so they must be in that adapter's dialect: Intl options for native, token strings for Luxon, date-fns and Moment. `MAT_DATE_LOCALE` defaults to Angular's `LOCALE_ID` and can be overridden, or changed later with `setLocale`. For German Luxon values: `provideLuxonDateAdapter(GERMAN_FORMATS)` and `{provide: MAT_DATE_LOCALE, useValue: 'de'}`. With date-fns the locale must be the `de` object from `date-fns/locale`, not a string.

code

ts · 17 lines
ts
import {Component, inject} from '@angular/core';
import {DateAdapter} from '@angular/material/core';
import {DateTime} from 'luxon';

@Component({
  selector: 'app-locale-switch',
  template: `<button (click)="useLocale('de')">Deutsch</button>
             <button (click)="useLocale('en-GB')">English</button>`,
})
export class LocaleSwitch {
  private readonly adapter = inject<DateAdapter<DateTime, string>>(DateAdapter);

  useLocale(code: string) {
    // Emits localeChanges: datepicker inputs and calendars re-render.
    this.adapter.setLocale(code);
  }
}

go deeper

for a junior

Recall that the datepicker needs an adapter, that the adapter decides the value type, and that formats and locale are separate tokens.

for a middle

Explain the MatDateFormats fields, why their values must match the adapter, the four provider functions and how MAT_DATE_LOCALE inherits LOCALE_ID.

for a senior

Show the traps: the date-fns Locale object, dialect mismatches that fail late, typing generics to the adapter, runtime setLocale and MatDatepickerIntl.

for a principal

Weigh which date library a product standardises on, given bundle size, immutability, time-zone needs and what the backend speaks, since the adapter choice spreads into every form.

## The adapter pattern behind the picker Angular Material's datepicker and timepicker are **date-implementation agnostic**. The calendar needs to know things like "what day of the week is the first of this month" or "how should this value appear in the input", but it never calls a date library itself. It asks an injected **`DateAdapter<D, L>`**, an abstract class from `@angular/material/core` whose generic `D` is the date type and `L` the locale type. Three injection tokens and classes cooperate: - **`DateAdapter`** creates, clones, compares, parses and formats values of type `D`, and turns ISO 8601 strings from a backend into `D` through `deserialize`. - **`MAT_DATE_FORMATS`** is a `MatDateFormats` object. Material reads a field such as `display.dateInput` and hands its value to `adapter.format(date, value)`; it never interprets the value itself. - **`MAT_DATE_LOCALE`** is the locale passed to the adapter. Its root default is `inject(LOCALE_ID)`, so it follows the application locale unless you provide something else. ## What each piece decides | Piece | Decides | Example for a German car-rental page | |---|---|---| | `DateAdapter` | value type, parsing, formatting, first day of week | `LuxonDateAdapter`, values are `DateTime` | | `MAT_DATE_FORMATS` | input text format, header label, accessible labels, time formats | `display.dateInput: 'dd.MM.yyyy'` | | `MAT_DATE_LOCALE` | month and weekday names, locale-sensitive tokens | `'de'` | The `MatDateFormats` shape is `parse.dateInput` (plus optional `parse.timeInput`) and `display.dateInput`, `monthYearLabel`, `dateA11yLabel`, `monthYearA11yLabel` (plus optional `monthLabel`, `timeInput`, `timeOptionLabel`). ## The four first-party adapters | Provider function | Package | Value type | Locale value | Default `display.dateInput` | |---|---|---|---|---| | `provideNativeDateAdapter()` | `@angular/material/core` | `Date` | locale code string | Intl options `{year, month, day: 'numeric'}` | | `provideDateFnsAdapter()` | `@angular/material-date-fns-adapter` | `Date` | date-fns `Locale` object | `'P'` | | `provideLuxonDateAdapter()` | `@angular/material-luxon-adapter` | Luxon `DateTime` | locale code string | `'D'` | | `provideMomentDateAdapter()` | `@angular/material-moment-adapter` | `Moment` | locale code string | `'l'` | Each function provides `DateAdapter` and `MAT_DATE_FORMATS` together and accepts your own `MatDateFormats` as its first argument. The three library adapters live in separate packages and are installed with `ng add`. ## Configuring German Luxon values ```ts import {ApplicationConfig} from '@angular/core'; import {MAT_DATE_LOCALE, MatDateFormats} from '@angular/material/core'; import {provideLuxonDateAdapter} from '@angular/material-luxon-adapter'; const GERMAN_FORMATS: MatDateFormats = { parse: {dateInput: ['dd.MM.yyyy', 'd.M.yyyy'], timeInput: 'HH:mm'}, display: { dateInput: 'dd.MM.yyyy', timeInput: 'HH:mm', monthYearLabel: 'LLL yyyy', dateA11yLabel: 'DDD', monthYearA11yLabel: 'LLLL yyyy', timeOptionLabel: 'HH:mm', }, }; export const appConfig: ApplicationConfig = { providers: [ provideLuxonDateAdapter(GERMAN_FORMATS), {provide: MAT_DATE_LOCALE, useValue: 'de'}, ], }; ``` The Luxon adapter tries ISO 8601 first, then each parse format in turn, so an array lets users type either the padded `12.10.2026` or the unpadded `1.9.2026`. ## Gotchas 1. **Dialect mismatch.** Intl option objects handed to the Luxon adapter, or Luxon tokens handed to the native adapter, do not throw at startup; they produce wrong text later. 2. **date-fns needs a `Locale` object.** The Material guide says so explicitly: provide `de` imported from `date-fns/locale`, because the adapter passes the value straight to date-fns functions. 3. **Generic types follow the adapter.** A `viewChild(MatDatepicker)` or a typed form control should use `DateTime` here, not `Date`. 4. **Runtime switching.** Injecting `DateAdapter` and calling `setLocale('fr')` emits `localeChanges`; the input re-renders its text and the calendar views re-render their names. 5. **Labels are separate.** Strings such as "Next month" or "Choose month and year" come from `MatDatepickerIntl`, which you subclass and provide; the locale does not translate them.

  • What changes if the same German page uses provideDateFnsAdapter() instead of Luxon?
    The value type becomes a plain `Date`, the format strings switch to date-fns tokens (the defaults are `'P'` and `'PP'`), and `MAT_DATE_LOCALE` must be the `de` `Locale` object imported from `date-fns/locale` rather than the string `'de'`. The adapter hands that value straight to date-fns `format` and `parse`, which expect a locale object.
  • How do you switch a datepicker from German to English at runtime without reloading the app?
    Inject `DateAdapter` and call `setLocale('en-GB')`. The adapter emits `localeChanges`; the datepicker input re-formats its text and the month, year and multi-year views re-render names. `MAT_DATE_LOCALE` is read only when the adapter is created, so changing the provider later does nothing.
  • The calendar shows German month names but its buttons still say 'Next month'. Why?
    Those strings are not locale data; they come from `MatDatepickerIntl` (`nextMonthLabel`, `switchToMultiYearViewLabel` and others). Provide a subclass with translated values, for example `{provide: MatDatepickerIntl, useClass: GermanDatepickerIntl}`, and call its `changes.next()` if you swap texts at runtime.

A DateAdapter works like a travel plug adapter. The datepicker has one fixed socket, and the adapter you fit decides which plug goes in: a Date, a Luxon DateTime or a Moment. Swap the adapter and the same datepicker takes a different plug, and the format strings you hand it have to suit that plug as well.

  • MAT_DATE_FORMATS uses one format syntax that every adapter understands.
  • The only way to change the datepicker's locale is to rebuild with a different LOCALE_ID.
  • With the Luxon adapter, the form control still receives JavaScript Date objects.
  • For the date-fns adapter you provide the string 'de' to MAT_DATE_LOCALE.
  • Setting MAT_DATE_LOCALE also translates the calendar's button labels.