Я исправил свою проблему. Я создал компонент с именем format-datepicker.
format-datepicker.ts:
import { NativeDateAdapter, DateAdapter, MAT_DATE_FORMATS, MatDateFormats } from "@angular/material";
import * as moment from 'moment';
export class AppDateAdapter extends NativeDateAdapter {
parse(value: any): Date | null {
if ((typeof value === 'string') && (value.indexOf('/') > -1)) {
const str = value.split('/');
const year = Number(str[2]);
const month = Number(str[1]) - 1;
const date = Number(str[0]);
return new Date(Date.UTC(year, month, date));
}
const timestamp = typeof value === 'number' ? value : Date.parse(value);
return isNaN(timestamp) ? null : new Date(timestamp);
}
createDate(year: number, month: number, date: number): Date {
return new Date(Date.UTC(year, month, date));
}
format(date: Date, displayFormat: Object): string {
let now = moment();
if (displayFormat === 'input') {
let day: string = date.getUTCDate().toString();
day = +day < 10 ? '0' + day : day;
let month: string = (date.getUTCMonth() + 1).toString();
month = +month < 10 ? '0' + month : month;
let year = date.getUTCFullYear();
return `${day}/${month}/${year}`;
}
// new Date(date.toDateString()).getUTCDate();
return date.toDateString();
}
private _to2digit(n: number) {
return (n);
//return ('00' + n).slice(-2);
}
}
export const APP_DATE_FORMATS =
{
parse: {
dateInput: {month: 'short', year: 'numeric', day: 'numeric'}
},
display: {
// dateInput: { month: 'short', year: 'numeric', day: 'numeric' },
dateInput: 'input',
// monthYearLabel: { month: 'short', year: 'numeric', day: 'numeric' },
monthYearLabel: 'inputMonth',
dateA11yLabel: {year: 'numeric', month: 'long', day: 'numeric'},
monthYearA11yLabel: {year: 'numeric', month: 'long'},
}
}
format-datepicker. html:
<mat-form-field>
<input matInput placeholder="Select Date" [matDatepicker]="datepickerRef" name="datepicker" ngModel #dateCtrl="ngModel" required readonly/>
<mat-datepicker-toggle [for]="datepickerRef" matSuffix></mat-datepicker-toggle>
<mat-datepicker #datepickerRef></mat-datepicker>
<mat-error *ngIf="dateCtrl.errors?.required && deptCtrl.touched">Choose date</mat-error>
</mat-form-field>
и в моем app.component.ts:
import {DateAdapter, MAT_DATE_FORMATS} from '@angular/material/core';
import { AppDateAdapter, APP_DATE_FORMATS } from '../format-datepicker/format-datepicker.component';
@Component({
selector: 'app-my selector',
templateUrl: './my page.html',
styleUrls: ['./my page.css'],
providers: [
{provide: DateAdapter, useClass: AppDateAdapter},
{provide: MAT_DATE_FORMATS, useValue: APP_DATE_FORMATS}
]
})