In angular 2 for md datepicker, you can change the date format as below:
Create a component which extends NativeDateAdapter:
import { Component, OnInit } from '@angular/core';
import { NativeDateAdapter } from '@angular/material';
export class DateAdapterComponent extends NativeDateAdapter {
format(date: Date, displayFormat: Object): string {
let day = date.getDate();
let month = date.getMonth();
let year = date.getFullYear();
if (displayFormat == "input") {
return this._toString(month) + ' '+ this._to2digit(day) + ',' + year;
} else {
return this._toString(month) + ' ' + year;
}
}
private _to2digit(n: number) {
return ('00' + n).slice(-2);
}
private _toString(n: number) {
let month = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
return month[n];
}
}
Add a date format constant in your app module:
const MY_DATE_FORMATS:MdDateFormats = {
parse: {
dateInput: {month: 'short', year: 'numeric', day: 'numeric'}
},
display: {
dateInput: 'input',
monthYearLabel: {year: 'numeric', month: 'short'},
dateA11yLabel: {year: 'numeric', month: 'long', day: 'numeric'},
monthYearA11yLabel: {year: 'numeric', month: 'long'},
}
};
and in provider, add date adapter and date formats:
providers: [ {provide: DateAdapter, useClass: DateAdapterComponent},
{provide: MD_DATE_FORMATS, useValue: MY_DATE_FORMATS}],