Created
April 18, 2019 15:10
-
-
Save wottpal/4211f4c01c41b16be181110273cff5a9 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) | |
} | |
} | |
Not really, just override the the format function. But couldn't find any example on the web and yours helped me. I wanted to share my variant just in case someone would need it. (I decided to keep most of my dates with the default javascript object for now. I don't have enough situations were a date library would be needed)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
had you made a new class 'AppDateAdapter' here?