24

I just tried to change the angular material 2 date-picker default Date Format MM/DD/YYYY to DD/MM/YYYY or DD.MM.YYYY or at least DD-MM-YYYY

According to this question and as mentioned in the documentation

providers: [{provide: MD_DATE_FORMATS, useValue: MY_NATIVE_DATE_FORMATS}]

So tried following approaches

Approach 1 plunkr : to get in DD-MM-YYYY format

Approach 2 plunkr : to get in DD.MM.YYYY format

Approach 3 plunkr : to get in DD/MM/YYYY format

but each of above approach working ordinary until I select a Date 1 to 12,

for ex: Today date is 25th September 2017, if I select 12th September 2017 as date, then once click datepicker button again I can see calender date, taken as 09th of November 2017(09/11/2017) not as (11/09/2017) , which is seems default date format not override correctly

5 Answers 5

92

Use this in your app.module.ts under the providers: array.

{provide: MAT_DATE_LOCALE, useValue: 'en-GB'}

8
  • 1
    Thanks man! I had the same problem and I used just 'nl' before. With 'nl-NL' it works properly.
    – Griffi
    Commented May 16, 2019 at 12:12
  • 2
    yep this does work, but you need to import to your app.module as well import { MAT_DATE_LOCALE } from '@angular/material';
    – bigtv
    Commented Sep 11, 2019 at 17:49
  • 1
    Great.. probably the easiest way...thanks for sharing
    – Buminda
    Commented Sep 26, 2019 at 23:45
  • 1
    It will help to change display value not actual Date value. I am getting Date value like this format "Fri Oct 30 2020 00:00:00 GMT+0600 (Bangladesh Standard Time)" Commented Oct 30, 2020 at 5:25
  • 1
    I have the same problem as explained. How can I set this if my app is standalone and doesn't have app.module.ts. Commented Oct 26, 2023 at 11:50
32

1/ DOCS: By cusomising the parse and display format with a custom date atapter

In the custom Date Adapter (yours is AppDateAdapter), add a parse method to parse the new date format (DD/MM/YYY) to a date valid date:

for example for the DD/MM/YYYY format, parse could be:

   parse(value: any): Date | null {
    if ((typeof value === 'string') && (value.indexOf('/') > -1)) {
      const str = value.split('/');
      const year = Number(str[2]);
      const month = Number(str[1]) - 1;
      const date = Number(str[0]);
      return new Date(year, month, date);
    }
    const timestamp = typeof value === 'number' ? value : Date.parse(value);
    return isNaN(timestamp) ? null : new Date(timestamp);
  }

working stackblitz: https://stackblitz.com/edit/angular-material-datepicker-format?embed=1&file=app/date.adapter.ts

your complete date adapter:

export class AppDateAdapter extends NativeDateAdapter {
    parse(value: any): Date | null {
        if ((typeof value === 'string') && (value.indexOf('/') > -1)) {
          const str = value.split('/');
          const year = Number(str[2]);
          const month = Number(str[1]) - 1;
          const date = Number(str[0]);
          return new Date(year, month, date);
        }
        const timestamp = typeof value === 'number' ? value : Date.parse(value);
        return isNaN(timestamp) ? null : new Date(timestamp);
      }
   format(date: Date, displayFormat: any): string {
       if (displayFormat == "input") {
           let day = date.getDate();
           let month = date.getMonth() + 1;
           let year = date.getFullYear();
           return this._to2digit(day) + '/' + this._to2digit(month) + '/' + year;
       } else {
           return date.toDateString();
       }
   }

   private _to2digit(n: number) {
       return ('00' + n).slice(-2);
   } 
}

The advantage of this approach is you could also custom the format of monthYearLabel in the display constants and could have a calendar which looks like:

enter image description here

7
  • thanks but changing this to 2017/06/28 does end up to weird situation.
    – Shilan
    Commented Oct 20, 2017 at 14:12
  • @Fetra R. okay, let's put it this way: i only changed this line return this._to2digit(day) + '/' + this._to2digit(month) + '/' + year; to year + '/'+ this._to2digit(day) + '/' + this._to2digit(month); Isn't it enough?
    – Shilan
    Commented Oct 20, 2017 at 14:34
  • This one that you just added is actually working! Thank you!
    – Shilan
    Commented Oct 20, 2017 at 14:38
  • This brokes if the input is numeric...would it be bad to add a "displayFormat" to save the selected format in the format method?
    – Sampgun
    Commented May 21, 2018 at 9:38
  • And why "displayFormat" is expected to be an Object if actually it's a string?
    – Sampgun
    Commented May 21, 2018 at 9:42
14

One of possible solution is simply defining your own input format:

export const DD_MM_YYYY_Format = {
    parse: {
        dateInput: 'LL',
    },
    display: {
        dateInput: 'DD/MM/YYYY',
        monthYearLabel: 'MMM YYYY',
        dateA11yLabel: 'LL',
        monthYearA11yLabel: 'MMMM YYYY',
    },
};

@Component({
    selector: 'my-app',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css'],
    providers: [
        {provide: MAT_DATE_FORMATS, useValue: DD_MM_YYYY_Format},
    ]
})
export class AppComponent implemets OnInit {
   // ... some code
}

The following code tells the injector to return a DD_MM_YYYY_Format when something asks for the MAT_DATE_FORMATS (read here for more details). Inside your custom format, property display.dateInput is set to DD/MM/YYYY.

8

Use it on your app.module.ts file under provider's section as below

 providers: [
    {provide: MAT_DATE_LOCALE, useValue: 'en-GB'}
  ], 
4

Updated version of Fetrarij, for date format dd.mm.yyyy with first day monday:

  • in app.module.ts:
 
    { provide: MAT_DATE_LOCALE, useValue: 'sk-SK' },
  • date adapter extension:
 

    @Injectable()
    export class DateAdapterSK extends NativeDateAdapter {

        getFirstDayOfWeek(): number {
            return 1;
        }

        parse(value: any): Date | null {
            if ((typeof value === 'string') && (value.indexOf('.') > -1)) {
                const str = value.split('.');
                const year = Number(str[2]);
                const month = Number(str[1]) - 1;
                const date = Number(str[0]);
                return new Date(year, month, date);
            }
            const timestamp = typeof value === 'number' ? value : Date.parse(value);
            return isNaN(timestamp) ? null : new Date(timestamp);
        }

    }

Work for me. Angular 6.

2
  • i a solved including part in only code app.module.ts {provider} Commented Apr 10, 2020 at 3:40
  • love this solution, just what i was looking for! luckily i just care about the display, and 'en-GB' works too!
    – csomakk
    Commented Feb 27, 2021 at 21:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.