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

Commit e175db3

Browse files
committed
fix(date filter): default to fullDate format
The browser's behave inconsistently, so we should just stick to one format when the format is not specified by the developer Closes #605
1 parent f38010d commit e175db3

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

src/filters.js

+22-20
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ var GET_TIME_ZONE = /[A-Z]{3}(?![+\-])/,
318318
* @param {(Date|number|string)} date Date to format either as Date object, milliseconds (string or
319319
* number) or ISO 8601 extended datetime string (yyyy-MM-ddTHH:mm:ss.SSSZ).
320320
* @param {string=} format Formatting rules (see Description). If not specified,
321-
* Date#toLocaleDateString is used.
321+
* `fullDate` is used.
322322
* @returns {string} Formatted string or the input if input is not recognized as date/millis.
323323
*
324324
* @example
@@ -344,7 +344,12 @@ var GET_TIME_ZONE = /[A-Z]{3}(?![+\-])/,
344344
</doc:example>
345345
*/
346346
angularFilter.date = function(date, format) {
347-
var $locale = this.$service('$locale');
347+
var $locale = this.$service('$locale'),
348+
text = '',
349+
parts = [],
350+
fn, match;
351+
352+
format = format || 'fullDate'
348353
format = $locale.DATETIME_FORMATS[format] || format;
349354
if (isString(date)) {
350355
if (NUMBER_STRING.test(date)) {
@@ -362,26 +367,23 @@ angularFilter.date = function(date, format) {
362367
return date;
363368
}
364369

365-
var text = date.toLocaleDateString(), fn;
366-
if (format && isString(format)) {
367-
text = '';
368-
var parts = [], match;
369-
while(format) {
370-
match = DATE_FORMATS_SPLIT.exec(format);
371-
if (match) {
372-
parts = concat(parts, match, 1);
373-
format = parts.pop();
374-
} else {
375-
parts.push(format);
376-
format = null;
377-
}
370+
while(format) {
371+
match = DATE_FORMATS_SPLIT.exec(format);
372+
if (match) {
373+
parts = concat(parts, match, 1);
374+
format = parts.pop();
375+
} else {
376+
parts.push(format);
377+
format = null;
378378
}
379-
forEach(parts, function(value){
380-
fn = DATE_FORMATS[value];
381-
text += fn ? fn(date, $locale.DATETIME_FORMATS)
382-
: value.replace(/(^'|'$)/g, '').replace(/''/g, "'");
383-
});
384379
}
380+
381+
forEach(parts, function(value){
382+
fn = DATE_FORMATS[value];
383+
text += fn ? fn(date, $locale.DATETIME_FORMATS)
384+
: value.replace(/(^'|'$)/g, '').replace(/''/g, "'");
385+
});
386+
385387
return text;
386388
};
387389

test/FiltersSpec.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,13 @@ describe('filter', function() {
225225
});
226226

227227
it('should do basic filter', function() {
228-
expect(date(noon)).toEqual(noon.toLocaleDateString());
229-
expect(date(noon, '')).toEqual(noon.toLocaleDateString());
228+
expect(date(noon)).toEqual(date(noon, 'fullDate'));
229+
expect(date(noon, '')).toEqual(date(noon, 'fullDate'));
230230
});
231231

232232
it('should accept number or number string representing milliseconds as input', function() {
233-
expect(date(noon.getTime())).toEqual(noon.toLocaleDateString());
234-
expect(date(noon.getTime() + "")).toEqual(noon.toLocaleDateString());
233+
expect(date(noon.getTime())).toEqual(date(noon.getTime(), 'fullDate'));
234+
expect(date(noon.getTime() + "")).toEqual(date(noon.getTime() + "", 'fullDate'));
235235
});
236236

237237
it('should accept various format strings', function() {
@@ -297,7 +297,7 @@ describe('filter', function() {
297297
it('should be able to parse ISO 8601 dates/times using', function() {
298298
var isoString = '2010-09-03T05:05:08.872Z';
299299
expect(date(isoString)).
300-
toEqual(angular.String.toDate(isoString).toLocaleDateString());
300+
toEqual(date(isoString, 'fullDate'));
301301
});
302302

303303
it('should parse format ending with non-replaced string', function() {

0 commit comments

Comments
 (0)