As per the official documents found here with a few minor tweaks: Found Here
Add the following to your app.module.ts / main module file imports section (View the display formats Here):
import { MatNativeDateModule, DateAdapter, MAT_DATE_LOCALE, MAT_DATE_FORMATS } from '@angular/material/core';
import { MomentDateModule, MomentDateAdapter, MAT_MOMENT_DATE_ADAPTER_OPTIONS } from '@angular/material-moment-adapter';
export const MY_FORMATS = {
parse: {
dateInput: 'LL',
},
display: {
dateInput: 'LL',
monthYearLabel: 'MMM YYYY',
dateA11yLabel: 'LL',
monthYearA11yLabel: 'MMMM YYYY',
},
};
@NgModule({
imports: [
MatDatepickerModule,
MatNativeDateModule,
MomentDateModule,
],
....
Add the following to your providers section in your app.module.ts / main module
providers: [
{
provide: DateAdapter,
useClass: MomentDateAdapter,
deps: [MAT_DATE_LOCALE, MAT_MOMENT_DATE_ADAPTER_OPTIONS]
},
{provide: MAT_DATE_FORMATS, useValue: MY_FORMATS},
],
Make sure to install moment.js:
npm install moment --save
In your component import the following:
import * as moment from 'moment';
In your component (reactive forms) bind your input fields, note the moment(your_start_date):
this.theForm = this.formBuilder.group({
startDate: [moment(this.service.filter.startDate), [Validators.required]],
endDate: [moment(this.service.filter.endDate), [Validators.required]]
})
});
Add this to your Template
<mat-form-field>
<mat-label>Date Range</mat-label>
<mat-date-range-input [rangePicker]="dateRangePicker" >
<input matStartDate formControlName="startDate" placeholder="Start date">
<input matEndDate formControlName="endDate" placeholder="End date">
</mat-date-range-input>
<mat-datepicker-toggle matSuffix [for]="dateRangePicker"></mat-datepicker-toggle>
<mat-date-range-picker #dateRangePicker></mat-date-range-picker>
</mat-form-field>