datepicker: not able to handle date year correctly on IE 11
Actual Behavior:
- What is the issue? * When I enter date via keyboard as 2/24/17 in date picker on Internet Explorer it evaluates date to 2/24/1917
- What is the expected behavior? * It should evaluate it to 2/24/2017
CodePen (or steps to reproduce the issue): *
CodePen Demo which shows your issue:https://codepen.io/anon/pen/gXwGNJ?editors=1010- Details: Though I have used the latest available angular material and angular version while re-creating the issue. My app is running on angular-material v1.0.9, angular v1.5.8. Since the issue is occurring in latest version of material I am assuming it must exists on older version too.
AngularJS Versions: *
AngularJS Version:1.5.5AngularJS Material Version:1.1.0
Additional Information:
Browser Type: *Internet Explorer 11Browser Version: *11.0.9600OS: *Windows 7 x64Stack Traces:
Since the issue is not allocated to anyone yet, I will post my solution that I used to get around the issue. I basically used the decorator and modified the function,
angular.module('ngMaterial').config(($provide) => {
$provide.decorator('$mdDateLocale', datePickerFormatter);
});
function datePickerFormatter ($delegate) {
$delegate.parseDate = (dateString) => {
let dateArray = dateString.split('/');
if (dateArray.length < 3) {
dateArray = dateString.split('-');
}
if (!dateArray[2]) {
return false;
}
const month = parseInt(dateArray[0], 10) - 1;
const day = dateArray[1];
const year = dateArray[2].length === 2 ? parseInt(dateArray[2], 10) + 2000 : dateArray[2];
return new Date(year, month, day);
};
return $delegate;
}
I don't think this is an issue.
parseDate should be all you need, the default just throws the input into a new Date()
https://github.com/angular/material/blob/master/src/components/datepicker/js/dateLocaleProvider.js#L188-L195
Should interpret date with ISO8601 so hyphens should work.
Right, the default parseDate is very basic and you will likely want to override it for your app. As @eknowles mentions above, you don't need to use a decorator, you just need to use $mdDateLocaleProvider in the config function as the $mdDateLocaleProvider Usage Docs demonstrate.
Here's your CodePen updated to do this more cleanly.