components icon indicating copy to clipboard operation
components copied to clipboard

[feature] Improve datepicker documentation for date format

Open Robouste opened this issue 9 years ago • 16 comments

Bug, feature request, or proposal:

The documentation is not clear on how to change the date format

What is the expected behavior?

Have a better example on how to achieve it

What is the current behavior?

It says it's easy but it's not

What is the use-case or motivation for changing an existing behavior?

Use the date picker on every application in the world

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Material beta.8 Angular 4.2.4

I tried to change the format to "dd MM yyyy" or "dd MMMM yyyy" but without any success.

Stackoverflow Angular 2 Material 2 datepicker date format

This post shows an example (which is incredibly complex for what we want to achieve) but it's not working.

Robouste avatar Jul 13 '17 09:07 Robouste

@Robouste Right now you have to write your own DateAdapter.. That is the only workaround that I found..

import {NativeDateAdapter} from '@angular/material';

export class MyDateAdapter extends NativeDateAdapter {

  format(date: Date, displayFormat: Object): string {
    if (displayFormat === 'input') {
      const day = date.getDate();
      const month = date.getMonth() + 1;
      const year = date.getFullYear();
      return `${year}-${month}-${day}`;
    } else {
      return date.toDateString();
    }
  }
...
}

For short day names add this lines(super hard coded at the moment, because I only need short names..):

getDayOfWeekNames(style): string[] {

    const SHORT_NAMES = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];

    return SHORT_NAMES;
  }

To return Monday as the first day of the week(add to MyDateAdapter class as well):

getFirstDayOfWeek(): number {
    return 1;
  }

Although the date formats I wasn't able to figure out, yet.. I tried:

const MY_DATE_FORMATS = {
  parse: {
    dateInput: {month: 'short', year: 'numeric', day: 'numeric'},
  },
  display: {
    dateInput: 'input',
    monthYearLabel: {year: 'numeric', month: 'numeric'},
    dateA11yLabel: {year: 'numeric', month: 'long', day: 'numeric'},
    monthYearA11yLabel: {year: 'numeric', month: 'long'},
  },
}; 

Then in app.module:

providers: [
    ...
    {provide: DateAdapter, useClass: MyDateAdapter},
    {provide: MD_DATE_FORMATS, useValue: MY_DATE_FORMATS},
    ...
  ],

I hope this helps..

benceHornyak avatar Jul 13 '17 12:07 benceHornyak

Ow, it actually worked to make the format like dd-MM-yyyy, thanks a lot !

The doc says :

Making the datepicker work with a different date implementation is as easy as extending DateAdapter, and using your subclass as the provider

I hate when they do that and say it's easy, it makes me feel like a very dumb person

Edit : do you mind if I post your answer on Stackoverflow ? It can help other people like me

Robouste avatar Jul 13 '17 12:07 Robouste

Welcome.. I think the document should include an example.. No, I don't mind, but please do link this issue somewhere in your answer

benceHornyak avatar Jul 13 '17 12:07 benceHornyak

Yes of course, thanks again

Robouste avatar Jul 13 '17 12:07 Robouste

@Robouste because DateAdapter is a generic abstract class, you can just specify the type of your custom date and extend it. The compiler would show you what you need to implement.

mxth avatar Jul 21 '17 04:07 mxth

@mxth

The compiler would show you what you need to implement.

I think the document should include an example..

I think updated documentation is better than having the compiler telling me what to implement

jarodsmk avatar Jul 25 '17 05:07 jarodsmk

While we are waiting for a better documentation, here is the equivalence in codes

  • Comments for documentation: https://github.com/angular/material2/blob/a5551081e7f045466ac94f511afabbff280ccaae/src/lib/core/datetime/date-adapter.ts
  • An example: https://github.com/angular/material2/blob/master/src/lib/core/datetime/native-date-adapter.ts

mxth avatar Jul 25 '17 06:07 mxth

this is about the display format

what about input and output format what if i dnt want my date to be time zone aware for example birthdate

i want to input my custom format and not iso

ankitraonka avatar Nov 14 '17 08:11 ankitraonka

You can write your own custom dateadapter if you need a different parsing format.

kuhnroyal avatar Nov 14 '17 11:11 kuhnroyal

Closing since the docs now has an example: https://material.angular.io/components/datepicker/examples (last example "Datepicker with custom formats")

mmalerba avatar Nov 14 '17 16:11 mmalerba

@mmalerba @kuhnroyal i have written my own adapter by extending native date adapter

fromIso8601(iso8601String: string): Date | null { return new Date(iso8601String); }

the above function ensures i can patch "11/13/2017, 4:09:46 PM" as input but when selecting fresh i still get iso. which function i should alter to get same locale output and not ISO when i do this.FormGroup.value

https://stackoverflow.com/questions/47262449/how-to-change-output-format-of-material-2-date-picker/47290841#47290841

ankitraonka avatar Nov 14 '17 18:11 ankitraonka

The stackblitz example of Datepicker is not working.

arifNislam avatar Jun 09 '18 22:06 arifNislam

Anyone was able to figure out how to format DD.MM.YYYY? As state before the verbose example on stackblitz is not working

f-aubert avatar Jun 25 '18 08:06 f-aubert

It's not working on Stackblitz due to https://github.com/angular/material2/issues/9780, you can see it working on the examples page of our documentation though, it's the same code: https://material.angular.io/components/datepicker/examples

mmalerba avatar Jul 02 '18 22:07 mmalerba

@Robouste Right now you have to write your own DateAdapter.. That is the only workaround that I found..

Hi, as far as you know, do I need to implement a solution like yours in Angular material 7 ? I included your snippet in my code but ts throws me error: ERROR in node_modules/@angular/material-moment-adapter/typings/adapter/moment-date-adapter.d.ts(10,24): error TS2307: Cannot find module 'moment'. , even if moment is installed

ghost avatar Oct 29 '18 16:10 ghost

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.