question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Datepicker Adapter not working as expected.

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:32 (14 by maintainers)

github_iconTop GitHub Comments

3reactions
Manasonabhabancommented, Apr 27, 2018

pic123

Can I load year view format. I do not want to load day. Please help me out in Angular 4.Thanks in advance.

3reactions
mmalerbacommented, Aug 18, 2017

@julianobrasil Yes you are on the right track. So Date.parse considers those to be valid dates and returns valid Date objects for them. In order to make it consider those invalid you would need to check for those cases and then return something that isValid will classify as an invalid date. I recommend doing this by just changing the parse function and leaving isValid alone, like this:

parse(value: any) {
  if (/* Check for strings I consider invalid */) {
    return new Date(NaN);
  }
  // continue constructing Date
}

Also I just wanted to say thank you for all of your help responding to datepicker issues 😃 I really appreciate it

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular Material Date-Picker is not working properly
This is the solution for my problem.I just run bellow code and now everything works fine as I expected, ng add @angular/material.
Read more >
Datepicker not working as expected - Essential Objects, Inc. Support ...
When I attempt to access SelectedDateString I get the following error: Unable to cast object of type 'System.String' to type 'EO.Web.DateCollection'. I have...
Read more >
Datepicker - Angular Material
The type of values that the datepicker expects depends on the type of DateAdapter provided in your application. The NativeDateAdapter , for example, ......
Read more >
Defect #13841: Today button in date picker does not work
It may not be necessary to change the upstream jQuery code, if trying to change the datePicker functionality. See the solution proposed by...
Read more >
pickdate and date picker not working (Accidentally deleted o
Expected behavior pickdate or date picker works. Actual behavior jquery.min.js:2 Uncaught TypeError: $(...).pickdate is not a function at HTMLDocument.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found