Datepicker Adapter not working as expected.
See original GitHub issueBug, 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: As you see above correct input but calendar doesn’t work. If the user try manually field is getting an error: But if I enter US format:
Which versions of Angular, Material, OS, TypeScript, browsers are affected?
Angular: 4.3.3 Material beta.8
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:32 (14 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Can I load year view format. I do not want to load day. Please help me out in Angular 4.Thanks in advance.
@julianobrasil Yes you are on the right track. So
Date.parse
considers those to be valid dates and returns validDate
objects for them. In order to make it consider those invalid you would need to check for those cases and then return something thatisValid
will classify as an invalid date. I recommend doing this by just changing theparse
function and leavingisValid
alone, like this:Also I just wanted to say thank you for all of your help responding to datepicker issues 😃 I really appreciate it