abidinberkay
abidinberkay

Reputation: 2025

How to change mat date picker date format in Angular?

I am using Angular mat date picker, its working good but the format of the date is not like what i want.

 <mat-form-field class="inputFieldForNewPolicy">
      <mat-label>Change details</mat-label>
      <input matInput name="selectedDate" [matDatepicker]="picker" placeholder="Choose a date" [(ngModel)]="selectedDate">
      <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
      <mat-datepicker #picker></mat-datepicker>
    </mat-form-field>

in ts file if i say:

sendButtonClicked() {
    console.log(this.selectedDate);
  }

the result comes as like:

Sat Aug 17 2019 00:00:00 GMT+0300 (Eastern European Summer Time)

I just want something like

17/08/2019

How can i change the format? In html or in ts?

Upvotes: 0

Views: 655

Answers (1)

Bharat Chauhan
Bharat Chauhan

Reputation: 3322

Three Four different type of setting. I have suggest app level (Indian / Britain) locale settings.

........
import { NgModule, LOCALE_ID } from '@angular/core';
.......

@NgModule({
  declarations: [
    .....
  ],
  imports: [
    ....
  ],
  providers: [
    ...
    { provide: LOCALE_ID, useValue: 'en-IN' } // en-GB
  ],
  exports: [...],
  bootstrap: [...]
})
export class AppModule {
}




import { DatePipe } from '@angular/common';
......

constructor(
        private datePipe: DatePipe
    ) {}

sendButtonClicked() {
    console.log(this.selectedDate);
    this.selectedDate = this.datePipe.transform(this.selectedDate, 'dd-MM-yyyy');
}

Upvotes: 1

Related Questions