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) | |
} | |
} | |
@gmcfarlane sorry, can’t help you with that. stopped using angular & material a while ago 🤷♂️
thanks for responding.
i was able to find a second article that was able to show me how to incorporate your work
(so it lives on!)
i find angular extremely slow to develop in, but as a classic MS asp.net guy, i do not want to fall into the abyss of old tech.
what do you use now?
greg
…-----Original Message-----
From: "Dennis Zoma" ***@***.***>
Sent: Thursday, September 29, 2022 12:48am
To: "wottpal" ***@***.***>
Cc: "gmcfarlane" ***@***.***>, "Mention" ***@***.***>
Subject: Re: wottpal/date-formats.ts
@wottpal commented on this gist.
[ @gmcfarlane ]( https://github.com/gmcfarlane ) sorry, can’t help you with that. stopped using angular & material a while ago 🤷♂️
—
Reply to this email directly, [ view it on GitHub ]( https://gist.github.com/4211f4c01c41b16be181110273cff5a9#gistcomment-4319139 ), or [ unsubscribe ]( https://github.com/notifications/unsubscribe-auth/AEUCMDNQUOYMIOZLQV4U43DWAUUSXANCNFSM6AAAAAAQYJU274 ).
You are receiving this because you were mentioned.Message ID: ***@***.***>
@gmcfarlane nextjs (react, typescript). never looked back after learning it – much nicer to work with imo.
ill take a look. i am pretty deep into this project trying to learn angular. i do not enjoy it.
…-----Original Message-----
From: "Dennis Zoma" ***@***.***>
Sent: Thursday, September 29, 2022 7:00am
To: "wottpal" ***@***.***>
Cc: "gmcfarlane" ***@***.***>, "Mention" ***@***.***>
Subject: Re: wottpal/date-formats.ts
@wottpal commented on this gist.
[ @gmcfarlane ]( https://github.com/gmcfarlane ) nextjs (react, typescript). never looked back after learning it – much nicer to work with imo.
—
Reply to this email directly, [ view it on GitHub ]( https://gist.github.com/4211f4c01c41b16be181110273cff5a9#gistcomment-4319456 ), or [ unsubscribe ]( https://github.com/notifications/unsubscribe-auth/AEUCMDONKYBP7RYNVE6JMP3WAWAHJANCNFSM6AAAAAAQYJU274 ).
You are receiving this because you were mentioned.Message ID: ***@***.***>
You saved my day. I used yours as inspiration. I wanted the default adapter just change the date format.
import {
DateAdapter,
MAT_DATE_FORMATS,
MAT_DATE_LOCALE,
NativeDateAdapter,
} from '@angular/material/core';
const DATE_FORMATS = {
parse: {
dateInput: 'dd.MM.yyyy',
},
display: {
dateInput: 'dd.MM.yyyy',
monthYearLabel: 'yyyy',
dateA11yLabel: 'dd.MM.yyyy',
monthYearA11yLabel: 'yyyy',
},
};
class AppDateAdapter extends NativeDateAdapter {
public constructor(matDateLocale: string) {
super(matDateLocale);
}
public format(date: Date, displayFormat: string): string {
if (displayFormat === 'dd.MM.yyyy') {
return date.toLocaleDateString('de-CH', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
});
} else {
return date.getFullYear().toString();
}
}
}
export const DateAdapterProviders = [
{ provide: MAT_DATE_FORMATS, useValue: DATE_FORMATS },
{
provide: DateAdapter,
useClass: AppDateAdapter,
deps: [MAT_DATE_LOCALE],
},
];
had you made a new class 'AppDateAdapter' here?
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
is there a stackblitz that shows this working? I am struggling getting the declarations right. Yes, newbie woes...