Skip to content

Commit 132454c

Browse files
committed
feat(filter): allow to define the timezone for formatting dates
Angular used to always use the browser timezone for `dateFilter`. An additional parameter was added to allow to use `UTC` timezone instead. Related to angular#8447.
1 parent ffbd276 commit 132454c

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/ng/filter/filters.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,8 @@ var DATE_FORMATS_SPLIT = /((?:[^yMdHhmsaZEw']+)|(?:'(?:[^']|'')*')|(?:E+|y+|M+|d
362362
* specified in the string input, the time is considered to be in the local timezone.
363363
* @param {string=} format Formatting rules (see Description). If not specified,
364364
* `mediumDate` is used.
365+
* @param {string=} timezone Timezone to be used for formatting. Rigth now, only `'UTC'` is supported.
366+
* If not specified, the timezone of the browser will be used.
365367
* @returns {string} Formatted string or the input if input is not recognized as date/millis.
366368
*
367369
* @example
@@ -417,7 +419,7 @@ function dateFilter($locale) {
417419
}
418420

419421

420-
return function(date, format) {
422+
return function(date, format, timezone) {
421423
var text = '',
422424
parts = [],
423425
fn, match;
@@ -447,6 +449,10 @@ function dateFilter($locale) {
447449
}
448450
}
449451

452+
if (timezone && timezone === 'UTC') {
453+
date = new Date(date.getTime());
454+
date.setMinutes(date.getMinutes() + date.getTimezoneOffset());
455+
}
450456
forEach(parts, function(value){
451457
fn = DATE_FORMATS[value];
452458
text += fn ? fn(date, $locale.DATETIME_FORMATS)

test/ng/filter/filtersSpec.js

+5
Original file line numberDiff line numberDiff line change
@@ -390,5 +390,10 @@ describe('filters', function() {
390390
expect(date('2003-09-10T13:02:03.12Z', format)).toEqual('2003-09-' + localDay + ' 03');
391391
expect(date('2003-09-10T13:02:03.1Z', format)).toEqual('2003-09-' + localDay + ' 03');
392392
});
393+
394+
it('should use UTC if the timzone is set to "UTC"', function() {
395+
expect(date(new Date(2003, 8, 10, 3, 2, 4), 'yyyy-MM-dd HH-mm-ss')).toEqual('2003-09-10 03-02-04');
396+
expect(date(new Date(Date.UTC(2003, 8, 10, 3, 2, 4)), 'yyyy-MM-dd HH-mm-ss', 'UTC')).toEqual('2003-09-10 03-02-04');
397+
});
393398
});
394399
});

0 commit comments

Comments
 (0)