components icon indicating copy to clipboard operation
components copied to clipboard

Datepicker Adapter not working as expected.

Open vapits opened this issue 9 years ago • 32 comments

Bug, feature request, or proposal:

Bug

What is the expected behavior?

Datepicker should use the LOCALE_ID or the NativeDateAdapter.

What is the current behavior?

Datepicker input has the correct date format though Calendar doesn't display and it works only if the user writes manually a US formatted date. Same with validators, if the user type a different than US format it returns error.

What are the steps to reproduce?

I have this MaterialModule in my app:

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

export class AppDateAdapter extends NativeDateAdapter {

  parse(value: any): Date | null {
    if ((typeof value === 'string') && (value.indexOf('-') > -1)) {
      const str = value.split('-');
      const dayArray = str[2].split('T');

      const year = Number(str[0]);
      const month = Number(str[1]) - 1;
      const day = Number(dayArray[0]);

      return new Date(year, month, day);
    }
    const timestamp = typeof value === 'number' ? value : Date.parse(value);
    return isNaN(timestamp) ? null : new Date(timestamp);
  }

  format(date: Date, displayFormat: Object): string {

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

@NgModule({
  exports: [
    MdModules,
  ],
  declarations: [],
  entryComponents: [
    PersonalDataDialogComponent
  ],
  providers: [
    { provide: DateAdapter, useClass: AppDateAdapter },
    { provide: MD_DATE_FORMATS, useValue: APP_DATE_FORMATS }

  ]
})
export class MaterialModule {
  constructor(private dateAdapter: DateAdapter<Date>) {
    dateAdapter.setLocale('nl-NL');
  }
}

I'm getting this on the input: image As you see above correct input but calendar doesn't work. If the user try manually field is getting an error: image But if I enter US format: image

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

Angular: 4.3.3 Material beta.8

vapits avatar Aug 14 '17 08:08 vapits

I implemented a similar approach for non American style dates and have similar behavior with an added quirk: The user enters 12-2-2017 being the 12th of Feb, the value returns correctly from the picker, however the visual acts odd, when clicking on the picker shows the month instead of the day, in my case December instead of Feb.

A simple format selector built into it would be GREAT.

Eppiox avatar Aug 14 '17 10:08 Eppiox

Here you can find a working example (DateAdapter) from @julianobrasil and the info about the planned solution https://github.com/angular/material2/issues/6263#issuecomment-320211085

ghd8 avatar Aug 14 '17 10:08 ghd8

@ghd8 it doesn't work on plnkr and if I add it in my project (I saw it already) it has exactly the same behavior. :/

vapits avatar Aug 14 '17 10:08 vapits

@vapits, have you called this.dateAdapter.setLocale('pt-br'); as I did in the plunk? It needs to be called once anywhere in the project (since the date adapter is a singleton). Or, even better, setup your LOCALE_ID. I've changed the plunk a little bit to show this possibility (look in the main.ts): https://plnkr.co/edit/jc4Ywu?p=preview

julianobrasil avatar Aug 14 '17 10:08 julianobrasil

Yes @julianobrasil and I have exactly the same behavior. :/

vapits avatar Aug 14 '17 10:08 vapits

@vapits, in the example you've posted above, you're not showing the parser function... it's the main part of all the process... do you have one in your real code?

julianobrasil avatar Aug 14 '17 10:08 julianobrasil

@julianobrasil just edited my code. I'm getting a value like: 2017-07-28T06:33:45.206Z which I want just to format like dd-MM-yyyy. It seems that it works only for the first display on the input only but actually manually the inputs waits for US style and same on Calendar.

vapits avatar Aug 14 '17 11:08 vapits

Modify my plunk and post it here with your date adapter. It's easier to see what's happening.

julianobrasil avatar Aug 14 '17 11:08 julianobrasil

The plnkr doesn't work for me on plnkr website it fails to load the packages :S

vapits avatar Aug 14 '17 11:08 vapits

ok... try to add this code to your date adapter:

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);
  }

julianobrasil avatar Aug 14 '17 11:08 julianobrasil

I did. I'm getting the calendar work but the parsed date is in wrong format: image

It doesn't respect it when I change it to dd-MM-yyyy

vapits avatar Aug 14 '17 11:08 vapits

oh, my bad... the right function is:

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);
  }

But note that it will mess up with the format function you've written, once it's output is different from what this parse function expects.

julianobrasil avatar Aug 14 '17 11:08 julianobrasil

@julianobrasil there are not changes on this function and mine.

vapits avatar Aug 14 '17 11:08 vapits

Yes, there are changes: I've changed value.indexOf('\') to value.indexOf('-') in the if statement.

julianobrasil avatar Aug 14 '17 11:08 julianobrasil

@julianobrasil Yes I already had fixed this :P :) Still I'm getting the screen shot above :/

vapits avatar Aug 14 '17 11:08 vapits

@julianobrasil sorry. The function is the same. But I have the APP_DATE__FORMATS. So if I have in them:

display: {
      dateInput: 'input',
      monthYearLabel: {year: 'numeric', month: 'short'},
      dateA11yLabel: {year: 'numeric', month: 'long', day: 'numeric'},
      monthYearA11yLabel: {year: 'numeric', month: 'long'},
  }

Then I have the same issue. Correct initial value but calendar and input (manual) waits for US input.

If I edit the dateInput to this: dateInput: { month: 'short', year: 'short', day: 'numeric' } then I get everything working but instead of dd-MM-yyyy I'm getting this Fri 29 July 2017 format which It's not my locale one neither my requirement. :/

vapits avatar Aug 14 '17 11:08 vapits

No matter what I do, it always works in the plunk :smiley: . I try using your code as it is (with de MD_DATE_FORMATS). At least how I imagine it is... It's in this plunk: https://plnkr.co/edit/rxzhhl?p=preview

julianobrasil avatar Aug 14 '17 11:08 julianobrasil

Can you try dateInput: {year: 'numeric', month: 'numeric', day: 'numeric'},

ghd8 avatar Aug 14 '17 11:08 ghd8

@ghd8 I tried that already no change :/ @julianobrasil where do you set the providers in your plnkr? And where are the APP_DATE_FORMATS. I'm copying the code exactly as yours and it doesn't work. Also try to use a date like the one I mention above as source: 2017-07-28T06:33:45.206Z and convert it to dd-MM-yyyy. It doesn't respect it. Only on the initial show.

vapits avatar Aug 14 '17 11:08 vapits

@vapits, I'm not messing with the APP_DATE_FORMATS. I've just did it in this plunk to leave it similar to your code.

But I think, from your last post, I didn't get it right. If you want to enter 2017-07-28T06:33:45.206Z (typing in the input, right?) you have to do almost nothing, but change the parse function to do something like new Date('2017-07-28T06:33:45.206Z'), or whatever date comes in from the args. Man, try to make the plunker to work.

And lets move this discussion to stackoverflow.com. There's way too many messages than recommended by the moderators and it really seems to be a misconfiguration of something instead of a bug.

Create a post in stackoverflow.com and post the link here.

julianobrasil avatar Aug 14 '17 11:08 julianobrasil