mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-07 08:57:54 +08:00
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.
This commit is contained in:
@@ -362,6 +362,8 @@ var DATE_FORMATS_SPLIT = /((?:[^yMdHhmsaZEw']+)|(?:'(?:[^']|'')*')|(?:E+|y+|M+|d
|
||||
* specified in the string input, the time is considered to be in the local timezone.
|
||||
* @param {string=} format Formatting rules (see Description). If not specified,
|
||||
* `mediumDate` is used.
|
||||
* @param {string=} timezone Timezone to be used for formatting. Right now, only `'UTC'` is supported.
|
||||
* If not specified, the timezone of the browser will be used.
|
||||
* @returns {string} Formatted string or the input if input is not recognized as date/millis.
|
||||
*
|
||||
* @example
|
||||
@@ -421,7 +423,7 @@ function dateFilter($locale) {
|
||||
}
|
||||
|
||||
|
||||
return function(date, format) {
|
||||
return function(date, format, timezone) {
|
||||
var text = '',
|
||||
parts = [],
|
||||
fn, match;
|
||||
@@ -451,6 +453,10 @@ function dateFilter($locale) {
|
||||
}
|
||||
}
|
||||
|
||||
if (timezone && timezone === 'UTC') {
|
||||
date = new Date(date.getTime());
|
||||
date.setMinutes(date.getMinutes() + date.getTimezoneOffset());
|
||||
}
|
||||
forEach(parts, function(value){
|
||||
fn = DATE_FORMATS[value];
|
||||
text += fn ? fn(date, $locale.DATETIME_FORMATS)
|
||||
|
||||
@@ -390,5 +390,10 @@ describe('filters', function() {
|
||||
expect(date('2003-09-10T13:02:03.12Z', format)).toEqual('2003-09-' + localDay + ' 03');
|
||||
expect(date('2003-09-10T13:02:03.1Z', format)).toEqual('2003-09-' + localDay + ' 03');
|
||||
});
|
||||
|
||||
it('should use UTC if the timezone is set to "UTC"', function() {
|
||||
expect(date(new Date(2003, 8, 10, 3, 2, 4), 'yyyy-MM-dd HH-mm-ss')).toEqual('2003-09-10 03-02-04');
|
||||
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');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user