components icon indicating copy to clipboard operation
components copied to clipboard

Change datepicker parse and display format at runtime

Open visurel opened this issue 6 years ago • 22 comments

Bug, feature request, or proposal:

Currently the parse and display format of a datepicker can only be set before runtime by providing them with provide: MAT_DATE_FORMATS, useValue: MY_DATE_FORMATS. As shown in the example: https://material.angular.io/components/datepicker/examples

What is the expected behavior?

The parse and display format of the datepicker should also be able to be changed at runtime, the same way it's possible to change the locale at runtime with .setLocale(). Maybe add .setDateFormat(MY_DATE_FORMAT_GENERATED_AT_RUNTIME), as sometimes the date format is only available after a HTTP request to the server.

What is the current behavior?

Date format is set in provider and cannot be changed at runtime.

What is the use-case or motivation for changing an existing behavior?

It should be possible to change the date format at runtime, to be able to react to date-formats, set by the backend. The user can set a preferred date format in the application and the application has to retrieve it from the server and adjust the datepicker accordingly. This is currently not possible.

visurel avatar Nov 10 '17 11:11 visurel

Or if you have a config file defining an interface with several datepicker components, each having a particular format (as they filter different kinds of data and need different precision). Currently, you need to create one component per format.

d-damien avatar Feb 23 '18 15:02 d-damien

Managed to get in a dirty hack to change the display dateInput dateformat after init of the component.

setTimeout(() => {
      const d = JSON.parse(JSON.stringify(this.picker._datepickerInput._dateFormats));
      d.display.dateInput = momentFormat;
      this.picker._datepickerInput._dateFormats = d;
});

This is in no way a good way of doing it - but it might help someone until the feature gets implemented.

marcgrabow avatar Mar 28 '18 07:03 marcgrabow

@marcgrabow, who is "this.picker"? how did you get a reference to this object?

Gil-Epshtain avatar Aug 01 '18 17:08 Gil-Epshtain

I've solve this problem by defining a CustomDateAdapter. You can find to full answer here: https://stackoverflow.com/questions/51634726/how-to-change-angular-material-datepicker-format-in-run-time/51696436#51696436

Gil-Epshtain avatar Aug 05 '18 17:08 Gil-Epshtain

@Gil-Epshtain sorry for the late response, didn't see your question. Probably not required anymore, but I just stored a reference to the picker just as a ViewChild, like this:

html:

<mat-datepicker #picker ></mat-datepicker>

ts:

@ViewChild('picker')
picker: any;

marcgrabow avatar Aug 28 '18 03:08 marcgrabow

Is it possible to make _dateFormats public in mat-datepicker-input? It should be possible to set custom datetime format for any datepicker.

Spaier avatar Dec 21 '18 18:12 Spaier

Any progress with this issue?

akvaliya avatar Mar 25 '19 10:03 akvaliya

@mmalerba I was wondering if you have an update on this issue?

LoganDupont avatar May 28 '19 13:05 LoganDupont

Changing the format at runtime is a default behaviour of a good datepicker plugin. This should be implemented soon.

marsc avatar Jun 14 '19 08:06 marsc

Any news on this?

marsc avatar Sep 05 '19 06:09 marsc

Need this feature as well

wweitzel avatar Oct 22 '19 17:10 wweitzel

I've solve this problem by creating another input which is displaying formatted date value. Check stackoverflow answer

Interesting approach, but it loses the ability to type a date in manually.

wweitzel avatar Oct 24 '19 14:10 wweitzel

The solution offered by @Gil-Epshtain works just fine. He posted a very detailed answer on StackOverflow and I have used something similar in my projects to great effect — although, you may want to improve his CustomDateAdapter by also overriding the parse method.

Have those asking for news on this understood his approach?

ccjmne avatar Oct 25 '19 12:10 ccjmne

@ccjmne I think that solution would change the format for every date picker in the entire app. The ideal solution here is just exposing a public way to set the format per date picker.

wweitzel avatar Oct 26 '19 13:10 wweitzel

This workaround of adding a custom input while disabling the original also can help: https://stackoverflow.com/a/58542346/3208614

caiofcm avatar May 31 '20 15:05 caiofcm

Addition to @marcgrabow great answer! I think it will be more meaningful instead of setTimeout to use zonejs instead in the ngOnInit of the datepicker component

 this.zone.onStable.pipe(
                take(1)
            ).subscribe(() => {
                const d = JSON.parse(JSON.stringify((this.picker._datepickerInput as any)._dateFormats));
                d.display.dateInput = datepickerOptions.format;
                d.parse.dateInput = datepickerOptions.format;
                (this.picker._datepickerInput as any)._dateFormats = d;
                 this.formControl.patchValue(datepickerOptions.defaultValue || moment());
            });

so I also added this line d.parse.dateInput = datepickerOptions.format; which will allow you to view the value inside the input with the given format.

mohamedelshorbagy avatar Aug 11 '20 14:08 mohamedelshorbagy

Mat-datepicker for Angular really has the most complicated realization I've ever seen. I met the same problem - I needed to place two datepickers in one view (one should show day, and the second should represent selected month). I couldn't find any normal solution. So finally I had to create a div representing second datepicker value in a way I need (Month + Year) and made it CSS position:absolute over the original value of my datepicker :) Some styling and voila ))) Looks like everything's OK. But of course it's not...

gspassky avatar Feb 24 '21 16:02 gspassky

Same problem.

NgocThachThai avatar Feb 26 '21 04:02 NgocThachThai

This is a feature my team would like to see

mridderikhoff avatar Oct 31 '22 22:10 mridderikhoff

I had the same needs for a project: use custom date formats and apply them instantly when the user switches the language within the application, and using different formats for different datepickers.

So waiting this feature to be available, I did a wrapper (really not perfect) around the Material DatePicker that seems to do the job for me for now. It's available here if it can help

gaewynn avatar Jan 05 '23 11:01 gaewynn

I also have a client project where the user can change the date format. At the moment we do a dirty reload. Please consider this issue for future roadmap.

Update: @Gil-Epshtain CustomDateAdapter is working for me 👍

marcowuethrich avatar Jun 09 '23 08:06 marcowuethrich

https://stackoverflow.com/a/65783137/2564847

robindijkhof avatar Dec 29 '23 20:12 robindijkhof