Skip to content

Commit 305d76c

Browse files
committed
fix(input): use UTC when parsing dates
Angular used to use the `new Date(month, date, year)` constructor to create `Date` objects in `input[type=date]` et al, which uses the current timezone of the browser. However, the HTML5 spec defines the timezone for for `input[type=date]` to be UTC. This commit changes the parsing to always use the UTC timezone. Closes angular#8447.
1 parent 1a05daf commit 305d76c

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

src/ng/directive/input.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,7 @@ function weekParser(isoWeek) {
10161016
week = +parts[2],
10171017
firstThurs = getFirstThursdayOfYear(year),
10181018
addDays = (week - 1) * 7;
1019-
return new Date(year, 0, firstThurs.getDate() + addDays);
1019+
return new Date(Date.UTC(year, 0, firstThurs.getDate() + addDays));
10201020
}
10211021
}
10221022

@@ -1045,7 +1045,7 @@ function createDateParser(regexp, mapping) {
10451045
}
10461046
});
10471047

1048-
return new Date(map.yyyy, map.MM - 1, map.dd, map.HH, map.mm);
1048+
return new Date(Date.UTC(map.yyyy, map.MM - 1, map.dd, map.HH, map.mm));
10491049
}
10501050
}
10511051

test/ng/directive/inputSpec.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -1643,7 +1643,7 @@ describe('input', function() {
16431643
it('should validate', function (){
16441644
changeInputValueTo('2013-07');
16451645
expect(inputElm).toBeValid();
1646-
expect(+scope.value).toBe(+new Date(2013, 6, 1));
1646+
expect(+scope.value).toBe(+Date.UTC(2013, 6, 1));
16471647
expect(scope.form.alias.$error.min).toBeFalsy();
16481648
});
16491649
});
@@ -1656,7 +1656,7 @@ describe('input', function() {
16561656
it('should validate', function (){
16571657
changeInputValueTo('2012-03');
16581658
expect(inputElm).toBeValid();
1659-
expect(+scope.value).toBe(+new Date(2012, 2, 1));
1659+
expect(+scope.value).toBe(+Date.UTC(2012, 2, 1));
16601660
expect(scope.form.alias.$error.max).toBeFalsy();
16611661
});
16621662

@@ -1684,7 +1684,7 @@ describe('input', function() {
16841684
compileInput('<input type="week" ng-model="secondWeek"/>');
16851685

16861686
scope.$apply(function(){
1687-
scope.secondWeek = new Date(2013, 0, 11);
1687+
scope.secondWeek = new Date(Date.UTC(2013, 0, 11));
16881688
});
16891689

16901690
expect(inputElm.val()).toBe('2013-W02');
@@ -1694,7 +1694,7 @@ describe('input', function() {
16941694
compileInput('<input type="week" ng-model="value"/>');
16951695

16961696
scope.$apply(function(){
1697-
scope.value = new Date(2013, 0, 11);
1697+
scope.value = new Date(Date.UTC(2013, 0, 11));
16981698
});
16991699

17001700

@@ -1738,7 +1738,7 @@ describe('input', function() {
17381738
compileInput('<input type="week" ng-model="test" />');
17391739

17401740
scope.$apply(function() {
1741-
scope.test = new Date(2011, 0, 1);
1741+
scope.test = new Date(Date.UTC(2011, 0, 1));
17421742
});
17431743

17441744
changeInputValueTo('');
@@ -1761,7 +1761,7 @@ describe('input', function() {
17611761
it('should validate', function (){
17621762
changeInputValueTo('2013-W03');
17631763
expect(inputElm).toBeValid();
1764-
expect(+scope.value).toBe(+new Date(2013, 0, 17));
1764+
expect(+scope.value).toBe(+Date.UTC(2013, 0, 17));
17651765
expect(scope.form.alias.$error.min).toBeFalsy();
17661766
});
17671767
});
@@ -1774,7 +1774,7 @@ describe('input', function() {
17741774
it('should validate', function (){
17751775
changeInputValueTo('2012-W01');
17761776
expect(inputElm).toBeValid();
1777-
expect(+scope.value).toBe(+new Date(2012, 0, 5));
1777+
expect(+scope.value).toBe(Date.UTC(2012, 0, 5));
17781778
expect(scope.form.alias.$error.max).toBeFalsy();
17791779
});
17801780

@@ -1878,7 +1878,7 @@ describe('input', function() {
18781878
it('should validate', function (){
18791879
changeInputValueTo('2000-01-01T23:02');
18801880
expect(inputElm).toBeValid();
1881-
expect(+scope.value).toBe(+new Date(2000, 0, 1, 23, 2));
1881+
expect(+scope.value).toBe(+Date.UTC(2000, 0, 1, 23, 2));
18821882
expect(scope.form.alias.$error.min).toBeFalsy();
18831883
});
18841884
});
@@ -1898,7 +1898,7 @@ describe('input', function() {
18981898
it('should validate', function() {
18991899
changeInputValueTo('2000-01-01T01:02');
19001900
expect(inputElm).toBeValid();
1901-
expect(+scope.value).toBe(+new Date(2000, 0, 1, 1, 2));
1901+
expect(+scope.value).toBe(+Date.UTC(2000, 0, 1, 1, 2));
19021902
expect(scope.form.alias.$error.max).toBeFalsy();
19031903
});
19041904
});
@@ -2023,7 +2023,7 @@ describe('input', function() {
20232023
it('should validate', function (){
20242024
changeInputValueTo('23:02');
20252025
expect(inputElm).toBeValid();
2026-
expect(+scope.value).toBe(+new Date(0, 0, 1, 23, 2));
2026+
expect(+scope.value).toBe(+Date.UTC(0, 0, 1, 23, 2));
20272027
expect(scope.form.alias.$error.min).toBeFalsy();
20282028
});
20292029
});
@@ -2043,7 +2043,7 @@ describe('input', function() {
20432043
it('should validate', function() {
20442044
changeInputValueTo('05:30');
20452045
expect(inputElm).toBeValid();
2046-
expect(+scope.value).toBe(+new Date(0, 0, 1, 5, 30));
2046+
expect(+scope.value).toBe(+Date.UTC(0, 0, 1, 5, 30));
20472047
expect(scope.form.alias.$error.max).toBeFalsy();
20482048
});
20492049
});
@@ -2168,7 +2168,7 @@ describe('input', function() {
21682168
it('should validate', function (){
21692169
changeInputValueTo('2000-01-01');
21702170
expect(inputElm).toBeValid();
2171-
expect(+scope.value).toBe(+new Date(2000, 0, 1));
2171+
expect(+scope.value).toBe(Date.UTC(2000, 0, 1));
21722172
expect(scope.form.alias.$error.min).toBeFalsy();
21732173
});
21742174
});
@@ -2188,7 +2188,7 @@ describe('input', function() {
21882188
it('should validate', function() {
21892189
changeInputValueTo('2000-01-01');
21902190
expect(inputElm).toBeValid();
2191-
expect(+scope.value).toBe(+new Date(2000, 0, 1));
2191+
expect(+scope.value).toBe(Date.UTC(2000, 0, 1));
21922192
expect(scope.form.alias.$error.max).toBeFalsy();
21932193
});
21942194
});

0 commit comments

Comments
 (0)