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

Commit cc82150

Browse files
committed
fix(date): parse string input as local time unless TZ is specified
previously we were always parsing the string input as UTC which cased issues like: {{ '2012-04-01' | date:'d MMM yyyy' }} renders as 31 Mar 2012 BREAKING CHANGE: string input without timezone info is now parsed as local time/date Closes #847
1 parent 037aefa commit cc82150

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

src/ng/filter/filters.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -330,18 +330,22 @@ function dateFilter($locale) {
330330

331331

332332
var R_ISO8601_STR = /^(\d{4})-?(\d\d)-?(\d\d)(?:T(\d\d)(?::?(\d\d)(?::?(\d\d)(?:\.(\d+))?)?)?(Z|([+-])(\d\d):?(\d\d))?)?$/;
333-
function jsonStringToDate(string){
333+
// 1 2 3 4 5 6 7 8 9 10 11
334+
function jsonStringToDate(string) {
334335
var match;
335336
if (match = string.match(R_ISO8601_STR)) {
336337
var date = new Date(0),
337338
tzHour = 0,
338-
tzMin = 0;
339+
tzMin = 0,
340+
dateSetter = match[8] ? date.setUTCFullYear : date.setFullYear,
341+
timeSetter = match[8] ? date.setUTCHours : date.setHours;
342+
339343
if (match[9]) {
340344
tzHour = int(match[9] + match[10]);
341345
tzMin = int(match[9] + match[11]);
342346
}
343-
date.setUTCFullYear(int(match[1]), int(match[2]) - 1, int(match[3]));
344-
date.setUTCHours(int(match[4]||0) - tzHour, int(match[5]||0) - tzMin, int(match[6]||0), int(match[7]||0));
347+
dateSetter.call(date, int(match[1]), int(match[2]) - 1, int(match[3]));
348+
timeSetter.call(date, int(match[4]||0) - tzHour, int(match[5]||0) - tzMin, int(match[6]||0), int(match[7]||0));
345349
return date;
346350
}
347351
return string;

test/ng/filter/filtersSpec.js

+19-14
Original file line numberDiff line numberDiff line change
@@ -253,32 +253,37 @@ describe('filters', function() {
253253
});
254254

255255

256-
it('should support various iso8061 date strings as input', function() {
257-
var format = 'yyyy-MM ss';
256+
it('should support various iso8061 date strings with timezone as input', function() {
257+
var format = 'yyyy-MM-dd ss';
258258

259259
//full ISO8061
260-
expect(date('2003-09-10T13:02:03.000Z', format)).toEqual('2003-09 03');
260+
expect(date('2003-09-10T13:02:03.000Z', format)).toEqual('2003-09-10 03');
261261

262-
expect(date('2003-09-10T13:02:03.000+00:00', format)).toEqual('2003-09 03');
262+
expect(date('2003-09-10T13:02:03.000+00:00', format)).toEqual('2003-09-10 03');
263263

264-
expect(date('2003-09-10T13:02:03-08:00', format)).toEqual('2003-09 03');
265-
266-
expect(date('20030910T033203-0930', format)).toEqual('2003-09 03');
267-
268-
//no timezone
269-
expect(date('2003-09-10T13:02:03.000', format)).toEqual('2003-09 03');
264+
expect(date('20030910T033203-0930', format)).toEqual('2003-09-10 03');
270265

271266
//no millis
272-
expect(date('2003-09-10T13:02:03Z', format)).toEqual('2003-09 03');
267+
expect(date('2003-09-10T13:02:03Z', format)).toEqual('2003-09-10 03');
273268

274269
//no seconds
275-
expect(date('2003-09-10T13:02Z', format)).toEqual('2003-09 00');
270+
expect(date('2003-09-10T13:02Z', format)).toEqual('2003-09-10 00');
276271

277272
//no minutes
278-
expect(date('2003-09-10T13Z', format)).toEqual('2003-09 00');
273+
expect(date('2003-09-10T13Z', format)).toEqual('2003-09-10 00');
274+
});
275+
276+
277+
it('should parse iso8061 date strings without timezone as local time', function() {
278+
var format = 'yyyy-MM-dd HH-mm-ss';
279+
280+
//full ISO8061 without timezone
281+
expect(date('2003-09-10T03:02:04.000', format)).toEqual('2003-09-10 03-02-04');
282+
283+
expect(date('20030910T030204', format)).toEqual('2003-09-10 03-02-04');
279284

280285
//no time
281-
expect(date('2003-09-10', format)).toEqual('2003-09 00');
286+
expect(date('2003-09-10', format)).toEqual('2003-09-10 00-00-00');
282287
});
283288

284289
it('should support different degrees of subsecond precision', function () {

0 commit comments

Comments
 (0)