question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Change datepicker parse and display format at runtime

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:46
  • Comments:19 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
marcgrabowcommented, Mar 28, 2018

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.

5reactions
Gil-Epshtaincommented, Aug 5, 2018

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Change date format in mat-datepicker dynamically
locale in a switch to do different date parsing by culture. I'm using Luxon, but the built-in "localized" formats aren't flexible/comprehensive ...
Read more >
Datepicker - Angular Material
The MAT_DATE_FORMATS object is a collection of formats that the datepicker uses when parsing and displaying dates. These formats are passed through to...
Read more >
How to change Angular Material Datepicker format in run-time
I'm working on an Angular App with Material Design, and I'm using Moment.js to parse and format dates. In one of my pages...
Read more >
Angular Material Datepicker Format - ConcretePage.com
Create a custom date format for parsing and displaying by Datepicker. ... export const MY_DATE_FORMATS = { parse: { dateInput: 'DD-MM-YYYY', }, ...
Read more >
Datepicker Widget - jQuery UI API Documentation
$.datepicker.parseDate( format, value, options ) · 'Invalid arguments' if either format or value is null · 'Missing number at position nn' if format...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found