Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include leading zeros in Angular Material Date Picker?

Using Angular Material 5.

I would like the date picker to display 01/01/2000 instead of 1/1/2000. I read the documentation, but I don't see anything regarding this.

Is there an easy way to implement this? Or will I ultimately have to create my own formatting somehow?

like image 627
masu9 Avatar asked Oct 27 '25 10:10

masu9


1 Answers

You can provide a custom implementation of MatDateFormats.

Add "@angular/material-moment-adapter" to your npm dependencies.

Add the following information to your root module or a separate material module that you then reference.

import { NgModule } from '@angular/core';
import {
    MatDatepickerModule,
    MAT_DATE_LOCALE,
    MAT_DATE_FORMATS,
    DateAdapter,
    MatDateFormats
    } from '@angular/material';
import {
    MomentDateAdapter
} from '@angular/material-moment-adapter';

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

@NgModule({
    imports: [
        MatDatepickerModule,
    ],
    exports: [
        MatDatepickerModule,
    ],
    providers: [
        { provide: MAT_DATE_LOCALE, useValue: 'en-US' },
        { provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE] },
        { provide: MAT_DATE_FORMATS, useValue: MY_FORMATS }
    ]
})
export class YourModule { }
like image 55
Igor Avatar answered Oct 29 '25 00:10

Igor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!