How to change Mat-Datepicker date format to DD/MM/YYYY in simplest way?

后端 未结 9 1550
孤城傲影
孤城傲影 2021-02-07 09:00

I\'m setting up a mat-datepicker for DOB and traditionally the display format is MM/DD/YYYY,I need to change it to DD/MM/YYYY with less coding

I tried format tag in mat

9条回答
  •  不知归路
    2021-02-07 09:15

    First, bind the format to your mat-datepicker.

    export const MY_FORMATS = {
        parse: {
            dateInput: 'LL'
        },
        display: {
            dateInput: 'YYYY-MM-DD',
            monthYearLabel: 'YYYY',
            dateA11yLabel: 'LL',
            monthYearA11yLabel: 'YYYY'
        }
    };
    

    along with this you need to import and provide the modules.

    import { DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE } from '@angular/material';
    import { MomentDateModule, MomentDateAdapter } from '@angular/material-moment-adapter';
    
    { provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE] },
    { provide: MAT_DATE_FORMATS, useValue: MY_FORMATS }
    

    And in HTML follow this simply.

    
        
        
        
    
    

提交回复
热议问题