Angular Material 5 – How to Customise date in mat-datepicker

angularangular-materialangular5

How do I customise mat-datepicker date in MM/DD/YYYY format?

I'm using following following configuration:

HTML:

<mat-form-field>
  <input matInput [matDatepicker]="picker" placeholder="Choose a date">
  <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
  <mat-datepicker #picker></mat-datepicker>
</mat-form-field>

TS:

import {Component} from '@angular/core';

/** @title Basic datepicker */
@Component({
  selector: 'datepicker-overview-example',
  templateUrl: 'datepicker-overview-example.html',
  styleUrls: ['datepicker-overview-example.css'],
})
export class DatepickerOverviewExample {}

When I print the form value it gives me following output:

Sun Dec 31 2017 00:00:00 GMT+0530 (IST)

I'm expecting format in MM/DD/YYYY.

I tried this configuration: https://material.angular.io/components/datepicker/overview#customizing-the-parse-and-display-formats

But it didn't work for me. I'm using default MatNativeDateModule (Not moment js)

Best Answer

You can use the Date Pipe as follows,

<input mdInput placeholder="Title" [ngModel]="mydate  | date:'MM-dd-yyyy'">

DEMO

Related Topic