Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

fix(input[date]): correctly parse 2-digit years #16539

Closed
wants to merge 2 commits into from

Conversation

gkalpak
Copy link
Member

@gkalpak gkalpak commented Apr 24, 2018

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

  • The commit message follows our guidelines
  • Fix/Feature: Docs have been added/updated
  • Fix/Feature: Tests have been added; existing tests pass

Other information:
Fixes #16537.

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);
Copy link
Contributor

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?

Copy link
Member Author

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.)

Copy link
Contributor

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).

@Narretz
Copy link
Contributor

Narretz commented Apr 27, 2018

2 of the new tests failed in Edge, otherwise LGTM

@gkalpak
Copy link
Member Author

gkalpak commented Apr 27, 2018

Edge does not support years less than 1000 (and sets the value to the empty strng instead 😞).
I'll exclude the tests.

Copy link
Contributor

@Narretz Narretz left a 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.

gkalpak added a commit that referenced this pull request Apr 27, 2018
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
@gkalpak gkalpak closed this in 627180f Apr 27, 2018
@gkalpak gkalpak deleted the fix-input-date-2digit-year branch May 5, 2018 10:06
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

input[date] year 0001 parse error - the model value updated to year 1901
4 participants