在角2 MD日期選擇器,您可以更改日期的格式如下:
創建延伸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];
}
}
添加日期格式常數您的應用程序模塊:
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'},
}
};
和供應商,添加日期適配器和日期格式:
providers: [ {provide: DateAdapter, useClass: DateAdapterComponent},
{provide: MD_DATE_FORMATS, useValue: MY_DATE_FORMATS}],
您正在使用哪個日期選擇器?你能鏈接嗎?如果你正在使用angular-material(angular 1.x),請參閱['$ mdDateLocaleProvider'](https://material.angularjs.org/latest/api/service/$mdDateLocaleProvider) – VincenzoC
@VincenzoC這是我正在使用的鏈接https://www.pincer.io/npm/libraries/angular2-material-datepicker –
您是否嘗試過使用'dateFormat'選項?你可以嘗試在你的'' –
VincenzoC