Overview
The <mat-datepicker> allows users to enter a date either through text input or by choosing a date from the calendar. It is made up of several components and directives that work together.
Basic Usage
import { Component } from '@angular/core';
import { MatDatepickerModule } from '@angular/material/datepicker';
import { MatNativeDateModule } from '@angular/material/core';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
@Component({
selector: 'app-datepicker-example',
standalone: true,
imports: [
MatFormFieldModule,
MatInputModule,
MatDatepickerModule,
MatNativeDateModule
],
template: `
<mat-form-field>
<mat-label>Choose a date</mat-label>
<input matInput [matDatepicker]="picker">
<mat-datepicker-toggle matIconSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker></mat-datepicker>
</mat-form-field>
`
})
export class DatepickerExample {}
API Reference
MatDatepicker
Selector: mat-datepicker
Exported as: matDatepicker
The date to open the calendar to initially.
startView
'month' | 'year' | 'multi-year'
default:"'month'"
The view that the calendar should start in.
Theme color of the datepicker’s calendar. This API is supported in M2 themes only.
Whether the calendar UI is in touch mode. In touch mode the calendar opens in a dialog rather than a dropdown.
Whether the datepicker pop-up should be disabled.
xPosition
'start' | 'end'
default:"'start'"
Preferred position of the datepicker in the X axis.
yPosition
'above' | 'below'
default:"'below'"
Preferred position of the datepicker in the Y axis.
Whether to restore focus to the previously-focused element when the calendar is closed.
dateClass
MatCalendarCellClassFunction<D>
Function that can be used to add custom CSS classes to dates.
Classes to be passed to the date picker panel.
Whether the calendar is open.
An input indicating the type of the custom header component for the calendar, if set.
Outputs
Emits selected year in multiyear view. This doesn’t imply a change on the selected date.
Emits selected month in year view. This doesn’t imply a change on the selected date.
viewChanged
EventEmitter<MatCalendarView>
Emits when the current view changes.
Emits when the datepicker has been opened.
Emits when the datepicker has been closed.
Methods
registerInput(input: C)
MatDateSelectionModel<S, D>
Registers an input with this datepicker.
Date Range Selection
<mat-form-field>
<mat-label>Enter a date range</mat-label>
<mat-date-range-input [rangePicker]="picker">
<input matStartDate placeholder="Start date">
<input matEndDate placeholder="End date">
</mat-date-range-input>
<mat-datepicker-toggle matIconSuffix [for]="picker"></mat-datepicker-toggle>
<mat-date-range-picker #picker></mat-date-range-picker>
</mat-form-field>
Accessibility
- The datepicker input uses
role="combobox" with appropriate ARIA attributes
- The calendar dialog uses
role="dialog" and is properly labeled
- Full keyboard navigation support:
ALT + DOWN_ARROW: Open the calendar
ESCAPE: Close the calendar
ARROW KEYS: Navigate between dates
PAGE_UP/PAGE_DOWN: Navigate between months
ENTER: Select the currently focused date
- Screen readers announce the selected date and navigation actions
- Use
aria-label on the datepicker toggle for better accessibility
Advanced Usage
import { MAT_DATE_FORMATS } from '@angular/material/core';
export const MY_DATE_FORMATS = {
parse: {
dateInput: 'DD/MM/YYYY',
},
display: {
dateInput: 'DD/MM/YYYY',
monthYearLabel: 'MMM YYYY',
dateA11yLabel: 'LL',
monthYearA11yLabel: 'MMMM YYYY',
},
};
@Component({
providers: [
{provide: MAT_DATE_FORMATS, useValue: MY_DATE_FORMATS}
]
})
export class DatepickerFormatsExample {}
Min and Max Dates
<mat-form-field>
<input matInput [matDatepicker]="picker" [min]="minDate" [max]="maxDate">
<mat-datepicker #picker></mat-datepicker>
</mat-form-field>
Date Validation
<mat-form-field>
<input matInput [matDatepicker]="picker" [matDatepickerFilter]="myFilter">
<mat-datepicker #picker></mat-datepicker>
</mat-form-field>
myFilter = (d: Date | null): boolean => {
const day = (d || new Date()).getDay();
// Prevent Saturday and Sunday from being selected.
return day !== 0 && day !== 6;
};
Touch UI Mode
For mobile devices, use touch UI mode:
<mat-datepicker touchUi="true"></mat-datepicker>