4

I want to change the default date format (YYYY-MM-DD) to some other format like (MM-DD-YYYY).

This is my calendar HTML structure.

 <mat-form-field class="full-width">
                        <input matInput [matDatepicker]="picker"
                               [(ngModel)]="currentDay"
                               (dateChange)="currentDayChanged()"
                               required
                               placeholder="date"
                               name="currentDay">
                        <mat-datepicker-toggle matSuffix
                                               [for]="picker">
                        </mat-datepicker-toggle>
                        <mat-datepicker #picker></mat-datepicker>
                    </mat-form-field>
miladhp
  • 141
  • 1
  • 2
  • 11
  • where you want to change the date? in view or in `.ts`during sending the date in api. if you want to change the date in view then you can follow #pardeep answer and if you want to change in .ts file then [follow this](https://stackoverflow.com/questions/41280149/how-to-use-angular2-built-in-date-pipe-in-services-and-directives-script-files) – Farhat Zaman Feb 13 '19 at 11:58
  • @FarhatZaman I just want to change in view. But it is not working. I'm working with the moment.js in ```.ts``` file. – miladhp Feb 13 '19 at 12:02
  • please share your `.ts` code as well and let us know what you have tried yet. and create a reproducible example in [stackblitz](https://stackblitz.com/), if possible. – Farhat Zaman Feb 13 '19 at 12:09
  • @FarhatZaman I will create it. – miladhp Feb 13 '19 at 12:15
  • Use a pipe where you are displaying date in html {{ element.createdDate | date: 'dd-MM-yyyy' }} – Cpt Kitkat Feb 14 '19 at 12:37
  • @saad Pipe is not working in the two way binding in the input field. – miladhp Feb 15 '19 at 06:31

2 Answers2

4

Set in AppModule your locale!

Example for Brazil,

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

import localePt from '@angular/common/locales/pt';
registerLocaleData(localePt, 'pt-BR');

@NgModule({
    declarations: [
        AppComponent
    ],
    imports: [],
    providers: [
      { provide: LOCALE_ID, useValue: 'pt-BR' }
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }
Neto Deolino
  • 79
  • 1
  • 5
  • I have implemented it and it works properly, but I have a problem. I want to get user date-format from one of the method in my Authentication service, How I should implement it dynamically? – miladhp Feb 15 '19 at 06:30
  • It works but I want to keep month names in english 'en-EN' and the date format is 'MM-DD-YYYY' – Eric Valero Dec 16 '20 at 07:38
3

There is a blog about it: https://blog.angular.io/taking-advantage-of-the-angular-material-datepicker-237e80fa14b3

(in case the URL becomes invalid), For example, say you’re already using Moment.js throughout your app, you can create a MomentDateAdapter:

Inherit a class

class MomentDateAdapter extends DateAdapter<Moment> {
  parse(value: any, parseFormat: any): Moment {
    return moment(value, parseFormat);
  }

  // Implementation for remaining abstract methods of DateAdapter.
}

Create a const like this, f.e. in a separate typescript file:

const MOMENT_FORMATS = {
  parse: {
    dateInput: 'LL',
  },
  display: {
    monthYearLabel: 'MMM YYYY',
    // See DateFormats for other required formats.
  },
};

finally provide both of these things in your application module

@NgModule({
  imports: [MdDatepickerModule, ...],
  providers: [
    {provide: DateAdapter, useClass: MomentDateAdapter},
    {provide: MD_DATE_FORMATS, useValue: MOMENT_FORMATS},
  ],
})
class AppModule {}

I appreciate if you let us know your results.

Bert Verhees
  • 1,057
  • 3
  • 14
  • 25
  • I have implemented it and it works properly, but I have a problem. I want to get user date-format from one of the method in my Authentication service, How I should implement it dynamically? – miladhp Feb 15 '19 at 06:25
  • I don't know the use case, but assume it is having an unknown number of dates, I would consider an array. – Bert Verhees Feb 15 '19 at 09:25