How can I customize date and time format in ngx-mat-datetime-picker?

27.2k Views Asked by At

I am working on Angular7 and its compatible ngx-mat-datetime-picker. It works as expected.

Want to customize the format:

Actual: mm/dd/yyyy, hh:mm:ss Expected: yyyy-mm-dd hh:mm:ss

Currently I don't find any option for formatting.

I tried something like this in template value="{{ mydate | date: 'yyyy-mm-dd hh:mm:ss' }}

But not working.

4

There are 4 best solutions below

8
On BEST ANSWER

You need to create a custom adapter and specify the custom formats

const CUSTOM_DATE_FORMATS: NgxMatDateFormats = {
  parse: {
    dateInput: 'l, LTS'
  },
  display: {
    dateInput: 'YYYY-MM-DD HH:mm:ss',
    monthYearLabel: 'MMM YYYY',
    dateA11yLabel: 'LL',
    monthYearA11yLabel: 'MMMM YYYY',
  }
};

@NgModule({
  providers: [
    {
      provide: NgxMatDateAdapter,
      useClass: CustomNgxDatetimeAdapter,
      deps: [MAT_DATE_LOCALE, NGX_MAT_MOMENT_DATE_ADAPTER_OPTIONS]
    },
    { provide: NGX_MAT_DATE_FORMATS, useValue: CUSTOM_DATE_FORMATS }
  ],
})
export class CustomNgxDateTimeModule { }

Please find the CustomNgxDatetimeAdapter.ts from the below gist

https://gist.github.com/nandhakumargdr/635af05419793e15f3758656ddd1ef39

enter image description here

Have tested it. It is working!

0
On

Create a constant: (insert your preferred format)

export const DATE_TIME_FORMAT = {
  parse: {
    dateInput: 'l, LT',
  },
  display: {
    dateInput: 'l, LT',
    monthYearLabel: 'MM yyyy',
    dateA11yLabel: 'LL',
    monthYearA11yLabel: 'MMMM YYYY',
  }

Provide:

{provide: NGX_MAT_DATE_FORMATS, useValue: DATE_TIME_FORMAT}

result (with my format removing the seconds):

9/4/2020, 1:11 PM
1
On
0
On

This is the best solution i found

import {
      // modules
      NgxMatDatetimePickerModule,
      NgxMatDateFormats,
      NGX_MAT_DATE_FORMATS,
    } from '@angular-material-components/datetime-picker';
    import {NgxMatMomentModule, NGX_MAT_MOMENT_DATE_ADAPTER_OPTIONS} from '@angular-material-components/moment-adapter';
    
    export const MOMENT_DATETIME_WITH_SECONDS_FORMAT = 'YYYY-MM-DD HH:mm:ss';
    
    // If using Moment
    const CUSTOM_MOMENT_FORMATS: NgxMatDateFormats = {
    parse: {
    dateInput: MOMENT_DATETIME_WITH_SECONDS_FORMAT,
    },
    display: {
    dateInput: MOMENT_DATETIME_WITH_SECONDS_FORMAT,
    monthYearLabel: 'MMM YYYY',
    dateA11yLabel: 'LL',
    monthYearA11yLabel: 'MMMM YYYY',
    },
    };
    
    @NgModule({
    declarations: [],
    imports: [
    MatDatepickerModule,
    MatMomentDateModule,
    NgxMatMomentModule,
    NgxMatDatetimePickerModule,
    ],
    exports: [
    MatDatepickerModule,
    MatMomentDateModule,
    NgxMatMomentModule,
    NgxMatDatetimePickerModule,
    ],
    providers: [
    // values
    {provide: MAT_MOMENT_DATE_ADAPTER_OPTIONS, useValue: {useUtc: true}},
    {provide: NGX_MAT_DATE_FORMATS, useValue: CUSTOM_MOMENT_FORMATS},
    {provide: NGX_MAT_MOMENT_DATE_ADAPTER_OPTIONS, useValue: {useUtc: true}},
    ],
    })
    export class AppModule {}