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

Commit 841013a

Browse files
committed
Add millisecond support for date filter
Date filter should translate input which is a number (or number string) into a date.
1 parent 4e9a2aa commit 841013a

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/filters.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,19 @@ var DATE_FORMATS = {
7373
}
7474
};
7575
var DATE_FORMATS_SPLIT = /([^yMdHhmsaZ]*)(y+|M+|d+|H+|h+|m+|s+|a|Z)(.*)/;
76+
var NUMBER_STRING = /^\d+$/;
7677

7778
angularFilter.date = function(date, format) {
78-
if (!(date instanceof Date)) return date;
79+
if (isString(date) && NUMBER_STRING.test(date)) {
80+
date = parseInt(date, 10);
81+
}
82+
83+
if (isNumber(date)) {
84+
date = new Date(date);
85+
} else if (!(date instanceof Date)) {
86+
return date;
87+
}
88+
7989
var text = date.toLocaleDateString(), fn;
8090
if (format && isString(format)) {
8191
text = '';

test/FiltersSpec.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,23 @@ describe('filter', function(){
9898
morning.getTimezoneOffset =
9999
noon.getTimezoneOffset =
100100
midnight.getTimezoneOffset =
101-
function() { return 7 * 60; };
101+
function() {return 7 * 60;};
102102

103103
it('should ignore falsy inputs', function() {
104104
expect(filter.date(null)).toEqual(null);
105105
expect(filter.date('')).toEqual('');
106-
expect(filter.date(123)).toEqual(123);
107106
});
108107

109108
it('should do basic filter', function() {
110109
expect(filter.date(noon)).toEqual(noon.toLocaleDateString());
111110
expect(filter.date(noon, '')).toEqual(noon.toLocaleDateString());
112111
});
113112

113+
it('should accept number or number string representing milliseconds as input', function() {
114+
expect(filter.date(noon.getTime())).toEqual(noon.toLocaleDateString());
115+
expect(filter.date(noon.getTime() + "")).toEqual(noon.toLocaleDateString());
116+
});
117+
114118
it('should accept format', function() {
115119
expect(filter.date(midnight, "yyyy-M-d h=H:m:saZ")).
116120
toEqual('2010-9-3 12=0:5:8am0700');
@@ -122,8 +126,6 @@ describe('filter', function(){
122126
toEqual('2010-09-03 12=12:05:08pm0700');
123127

124128
});
125-
126-
127129
});
128130
});
129131

0 commit comments

Comments
 (0)