Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change Mat-Datepicker date format to DD/MM/YYYY in simplest way?

I'm setting up a mat-datepicker for DOB and traditionally the display format is MM/DD/YYYY,I need to change it to DD/MM/YYYY with less coding

I tried format tag in mat-date picker but it does not work,other date pickers like ngbDatepicker format can easily changed through one line of coding

  <div class="col-md-3 Dinline-block">      <mat-form-field>         <input matInput [matDatepicker]="picker2" [max]="currentDate"           placeholder="DOB(DD/MM/YYYY)" required formControlName="dob"            readonly />         <mat-datepicker-toggle matSuffix [for]="picker2"></mat-datepicker-           toggle>         <mat-datepicker #picker2></mat-datepicker>      </mat-form-field>   </div> 

The date displayed after selecting from mat-date picker should be DD-MM-YYYY and when the retrieving the value from date picker it should also be in DD-MM-YYYY format

like image 813
Zeeshan Chaudhary Avatar asked Apr 17 '19 06:04

Zeeshan Chaudhary


People also ask

How do I change date format in picker?

Do one of the following: For a text box control or a date picker control, ensure that the Data type list displays the appropriate data type, and then click Format. For an expression box control, ensure that the Format as list displays the appropriate data type, and then click Format.

How do I change Datepicker format from DD MM to YYYY?

var year = 2014; var month = 5; var day = 10; var realDate = new Date(year, month - 1, day); // months are 0-based! $('#MainContent_txtDataConsegna'). datepicker({ dateFormat: 'dd/mm/yyyy' }); // format to show $('#MainContent_txtDataConsegna'). datepicker('setDate', realDate);

How do you use a mat date range picker?

mat-datepicker exampleAdd a template reference variable mat-datepicker element. Connect mat-datepicker to input element via [matDatepicker] property. Finally add date picker toggle button to display or hide calender popup by using mat-datepicker-toggle element.


Video Answer


1 Answers

The easiest way is to change the locale:

Add the following to the providers section of your module:

{ provide: MAT_DATE_LOCALE, useValue: 'en-GB' } 
like image 190
progm Avatar answered Sep 23 '22 13:09

progm