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

input[date] year 0001 parse error - the model value updated to year 1901 #16537

Closed
1 of 3 tasks
Kienz opened this issue Apr 24, 2018 · 1 comment · Fixed by angular-indonesia/angular.js#133
Closed
1 of 3 tasks

Comments

@Kienz
Copy link

Kienz commented Apr 24, 2018

I'm submitting a ...

  • bug report
  • feature request
  • other

Current behavior:
If you select the year 0001 in a input[date] field, the model values updates to the value 1901.
https://codepen.io/anon/pen/KRzwXX

Expected / new behavior:
The model value should be set to the correct year 0001.

Minimal reproduction of the problem with instructions:
See codepen testcase https://codepen.io/anon/pen/KRzwXX
Insert the date '0001-01-01' and you can see the wrong model value.

AngularJS version: 1.6.9 / 1.7.0-rc.0

Browser: [all]
Also happens if the browser doesn't support input[date] fields. If the text '0001-01-01' is inserted, the model values is set to '1901-01-01'

@gkalpak
Copy link
Member

gkalpak commented Apr 24, 2018

Interesting that nobody has noticed all these years 😁
This happens for all year values less than 100, because we call new Date(1, ...) and according to MDN:

Values from 0 to 99 map to the years 1900 to 1999.
[...]
In order to create dates between the years 0 and 99 the Date.prototype.setFullYear() method should be used.

gkalpak added a commit to gkalpak/angular.js that referenced this issue Apr 24, 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 angular#16537
gkalpak added a commit that referenced this issue 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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
2 participants