Angular 2 Material 2 datepicker date format

前端 未结 14 2274
悲哀的现实
悲哀的现实 2020-11-28 23:21

I need help. I don\'t know how to change the date format of the material 2 datepicker. I\'ve read documentation but I don\'t understand what I actually need to do. Output da

14条回答
  •  再見小時候
    2020-11-28 23:50

    I used the solution propose by @igor-janković and had the problem of "Error: Uncaught (in promise): TypeError: Cannot read property 'dateInput' of undefined". I realized that this problem was because MY_DATE_FORMATS need to be declare like type MdDateFormats:

    export declare type MdDateFormats = {
        parse: {
            dateInput: any;
        };
        display: {
            dateInput: any;
            monthYearLabel: any;
            dateA11yLabel: any;
            monthYearA11yLabel: any;
        };
    };
    

    So, the correct way of declare MY_DATE_FORMATS is:

    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'},
       }
    };
    

    With the above modification the solution work for me.

    Regards

提交回复
热议问题