Skip to content
This repository was 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
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -1415,7 +1415,7 @@ function weekParser(isoWeek, existingDate) {
}

function createDateParser(regexp, mapping) {
return function(iso, date) {
return function(iso, previousDate) {
var parts, map;

if (isDate(iso)) {
Expand All @@ -1437,15 +1437,15 @@ function createDateParser(regexp, mapping) {

if (parts) {
parts.shift();
if (date) {
if (previousDate) {
map = {
yyyy: date.getFullYear(),
MM: date.getMonth() + 1,
dd: date.getDate(),
HH: date.getHours(),
mm: date.getMinutes(),
ss: date.getSeconds(),
sss: date.getMilliseconds() / 1000
yyyy: previousDate.getFullYear(),
MM: previousDate.getMonth() + 1,
dd: previousDate.getDate(),
HH: previousDate.getHours(),
mm: previousDate.getMinutes(),
ss: previousDate.getSeconds(),
sss: previousDate.getMilliseconds() / 1000
};
} else {
map = { yyyy: 1970, MM: 1, dd: 1, HH: 0, mm: 0, ss: 0, sss: 0 };
Expand All @@ -1456,7 +1456,15 @@ function createDateParser(regexp, mapping) {
map[mapping[index]] = +part;
}
});
return new Date(map.yyyy, map.MM - 1, map.dd, map.HH, map.mm, map.ss || 0, map.sss * 1000 || 0);

var date = new Date(map.yyyy, map.MM - 1, map.dd, map.HH, map.mm, map.ss || 0, map.sss * 1000 || 0);
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
Collaborator

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
Collaborator

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

}

return date;
}
}

Expand Down
43 changes: 35 additions & 8 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@
/* globals generateInputCompilerHelper: false */

describe('input', function() {
var helper = {}, $compile, $rootScope, $browser, $sniffer, $timeout, $q;
var helper = {}, $compile, $rootScope, $browser, $sniffer;

// UA sniffing to exclude Edge from some date input tests
var isEdge = /\bEdge\//.test(window.navigator.userAgent);

generateInputCompilerHelper(helper);

beforeEach(inject(function(_$compile_, _$rootScope_, _$browser_, _$sniffer_, _$timeout_, _$q_) {
beforeEach(inject(function(_$compile_, _$rootScope_, _$browser_, _$sniffer_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
$browser = _$browser_;
$sniffer = _$sniffer_;
$timeout = _$timeout_;
$q = _$q_;
}));


Expand Down Expand Up @@ -1556,6 +1554,20 @@ describe('input', function() {

expect(inputElm).toBeValid();
});


it('should correctly handle 2-digit years', function() {
helper.compileInput('<input type="datetime-local" ng-model="value" name="alias" />');

helper.changeInputValueTo('0001-01-01T12:34:00');
expect($rootScope.value.getFullYear()).toBe(1);

helper.changeInputValueTo('0099-01-01T12:34:00');
expect($rootScope.value.getFullYear()).toBe(99);

helper.changeInputValueTo('0100-01-01T12:34:00');
expect($rootScope.value.getFullYear()).toBe(100);
});
});


Expand Down Expand Up @@ -2323,9 +2335,9 @@ describe('input', function() {

it('should allow Date objects as valid ng-max values', function() {
$rootScope.max = new Date(2012, 1, 1, 1, 2, 0);
var inputElm = helper.compileInput('<input type="datetime-local" ng-model="value" name="alias" ng-max="max" />');
var inputElm = helper.compileInput('<input type="date" ng-model="value" name="alias" ng-max="max" />');

helper.changeInputValueTo('2014-01-01T12:34:00');
helper.changeInputValueTo('2014-01-01');
expect(inputElm).toBeInvalid();

$rootScope.max = new Date(2013, 1, 1, 1, 2, 0);
Expand All @@ -2342,9 +2354,9 @@ describe('input', function() {

it('should allow Date objects as valid ng-min values', function() {
$rootScope.min = new Date(2013, 1, 1, 1, 2, 0);
var inputElm = helper.compileInput('<input type="datetime-local" ng-model="value" name="alias" ng-min="min" />');
var inputElm = helper.compileInput('<input type="date" ng-model="value" name="alias" ng-min="min" />');

helper.changeInputValueTo('2010-01-01T12:34:00');
helper.changeInputValueTo('2010-01-01');
expect(inputElm).toBeInvalid();

$rootScope.min = new Date(2014, 1, 1, 1, 2, 0);
Expand All @@ -2358,6 +2370,21 @@ describe('input', function() {
expect(inputElm).toBeValid();
});


it('should correctly handle 2-digit years', function() {
helper.compileInput('<input type="date" ng-model="value" name="alias" />');

helper.changeInputValueTo('0001-01-01');
expect($rootScope.value.getFullYear()).toBe(1);

helper.changeInputValueTo('0099-01-01');
expect($rootScope.value.getFullYear()).toBe(99);

helper.changeInputValueTo('0100-01-01');
expect($rootScope.value.getFullYear()).toBe(100);
});


describe('ISO_DATE_REGEXP', function() {
var dates = [
// Validate date
Expand Down