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

Commit 4739b1d

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 #8447.
1 parent feed7d6 commit 4739b1d

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. Right 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
@@ -421,7 +423,7 @@ function dateFilter($locale) {
421423
}
422424

423425

424-
return function(date, format) {
426+
return function(date, format, timezone) {
425427
var text = '',
426428
parts = [],
427429
fn, match;
@@ -451,6 +453,10 @@ function dateFilter($locale) {
451453
}
452454
}
453455

456+
if (timezone && timezone === 'UTC') {
457+
date = new Date(date.getTime());
458+
date.setMinutes(date.getMinutes() + date.getTimezoneOffset());
459+
}
454460
forEach(parts, function(value){
455461
fn = DATE_FORMATS[value];
456462
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 timezone 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)