-
-
Save shalaby/762cd2a60029641f52ef42b644ec23a2 to your computer and use it in GitHub Desktop.
Custom DateFormats & DateAdapter for Angular Material MatDatepicker using Day.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Platform } from '@angular/cdk/platform'; | |
import { NativeDateAdapter } from '@angular/material'; | |
import * as dayjs from 'dayjs'; | |
import 'dayjs/locale/de'; | |
import * as customParseFormat from 'dayjs/plugin/customParseFormat'; | |
import * as localizedFormat from 'dayjs/plugin/localizedFormat'; | |
/** | |
* Custom Date-Formats and Adapter (using https://github.com/iamkun/dayjs) | |
*/ | |
export const AppDateFormats = { | |
parse: { | |
dateInput: 'DD.MM.YYYY', | |
}, | |
display: { | |
dateInput: 'DD.MM.YYYY', | |
monthYearLabel: 'MMM YYYY', | |
dateA11yLabel: 'LL', | |
monthYearA11yLabel: 'MMMM YYYY', | |
} | |
} | |
export class AppDateAdapter extends NativeDateAdapter { | |
constructor(matDateLocale: string, platform: Platform) { | |
super(matDateLocale, platform) | |
// Initalize DayJS-Parser | |
dayjs.locale('de') | |
dayjs.extend(customParseFormat) | |
dayjs.extend(localizedFormat) | |
} | |
parse(value: any): Date | null { | |
return dayjs(value, 'DD.MM.YYYY').toDate() | |
} | |
format(date: Date, displayFormat: any): string { | |
return dayjs(date).format(displayFormat) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment