Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

md-datepicker input format

When using the md-datepicker directive in angular material, the date format doesn't seem to work for direct input.

If I select a date in the calendar, it will be displayed as specified (in my case DD-MM-YYYY) but if I try to change the input manually, my entry is analysed as MM-DD-YYYY.

So far, my datepicker is set using this code from this question

angular.module('MyApp').config(function($mdDateLocaleProvider) {
  $mdDateLocaleProvider.formatDate = function(date) {
    return date ? moment(date).format('DD-MM-YYYY') : '';
  };
});

Here is a codepen to see the problem in action.

Is there a way to setup the entry format?

like image 785
Dorian M Avatar asked Nov 02 '15 10:11

Dorian M


People also ask

How to change the date format in mat datepicker?

mat-datepicker date formatImport formatDate from @angular/common to change the datepicker input format. Add custom date picker formats (PICK_FORMATS). Finally add the custom date adapter and custom date formats to the provider array of component to overwrite default DateAdapter and MAT_DATE_FORMATS.

What is MD datepicker?

The md-datepicker, an Angular Directive, is an input control to select a date and supports ngMessages for input validation.

How do I import MatDatepickerModule?

After completing the installation, Import ' MatDatepickerModule,' from '@angular/material' in the app. module. ts file. Then use <mat-datepicker-toggle> tag to use angular material date picker and as well as matInput.


1 Answers

Format date event is not enough. You should also configure parse date event.

$mdDateLocaleProvider.parseDate = function(dateString) {
  var m = moment(dateString, 'DD-MM-YYYY', true);
  return m.isValid() ? m.toDate() : new Date(NaN);
};

See updated pen: http://codepen.io/anon/pen/GpBpwZ?editors=101

like image 77
Kursad Gulseven Avatar answered Nov 13 '22 14:11

Kursad Gulseven