-
Notifications
You must be signed in to change notification settings - Fork 27.5k
fix(input[date]): correctly parse 2-digit years #16539
Conversation
When parsing a date string value, AngularJS uses `new Date(year, ...)` to create a Date object and assign it to the model. In the constructor, 2-digit years map to 1900-1999, so the created Date object has the wrong value for year. This commit fixes it, by explicitly using `setFullYear()` to set the year to the correct value, when necessary. Fixes angular#16537
if (map.yyyy < 100) { | ||
// In the constructor, 2-digit years map to 1900-1999. | ||
// Use `setFullYear()` to set the correct year. | ||
date.setFullYear(map.yyyy); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about putting this before the new Date
and doing map.yyyy += 1900
? Then the return new Date(...)
can be left as-is and maybe the date
=> previousDate
change isn't needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But we don't want the year to be +1900 😁
(Also, the date
--> previousDate
wasn't really necessary, but I thought previousDate
conveys the nature of the argument better 😁 Happy to change it back.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But we don't want the year to be +1900 😁
Right, that's basically what we are preventing...
I'm fine keeping the previousDate
change if the new variable is needed, which it appears it is (unlike what I first thought).
2 of the new tests failed in Edge, otherwise LGTM |
Edge does not support years less than 1000 (and sets the value to the empty strng instead 😞). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not surprising. I remember that we had the same issue with years with more than 4 digits.
When parsing a date string value, AngularJS uses `new Date(year, ...)` to create a Date object and assign it to the model. In the constructor, 2-digit years map to 1900-1999, so the created Date object has the wrong value for year. This commit fixes it, by explicitly using `setFullYear()` to set the year to the correct value, when necessary. Fixes #16537 Closes #16539
What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
Bug fix.
What is the current behavior? (You can also link to an open issue here)
When parsing a date string value, AngularJS uses
new Date(year, ...)
to create a Date object and assign it to the model. In the constructor, 2-digit years map to 1900-1999, so the created Date object has the wrong value for year.What is the new behavior (if this is a feature change)?
setFullYear()
is explicitly called to set the year to the correct value, when necessary.Does this PR introduce a breaking change?
No.
Please check if the PR fulfills these requirements
Fix/Feature: Docs have been added/updatedOther information:
Fixes #16537.