aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIgor Minar2010-10-23 18:44:32 -0700
committerIgor Minar2010-10-25 10:44:03 -0700
commit841013a4c4d25acf6fc9ff40e449c3d0a4b82ec3 (patch)
treef01cdcc51efdab4d30566dc838de203317ef5531
parent4e9a2aa10ec6e84377aebd37fd3ae44af3d9423a (diff)
downloadangular.js-841013a4c4d25acf6fc9ff40e449c3d0a4b82ec3.tar.bz2
Add millisecond support for date filter
Date filter should translate input which is a number (or number string) into a date.
-rw-r--r--src/filters.js12
-rw-r--r--test/FiltersSpec.js10
2 files changed, 17 insertions, 5 deletions
diff --git a/src/filters.js b/src/filters.js
index 5b734882..103cd2de 100644
--- a/src/filters.js
+++ b/src/filters.js
@@ -73,9 +73,19 @@ var DATE_FORMATS = {
}
};
var DATE_FORMATS_SPLIT = /([^yMdHhmsaZ]*)(y+|M+|d+|H+|h+|m+|s+|a|Z)(.*)/;
+var NUMBER_STRING = /^\d+$/;
angularFilter.date = function(date, format) {
- if (!(date instanceof Date)) return date;
+ if (isString(date) && NUMBER_STRING.test(date)) {
+ date = parseInt(date, 10);
+ }
+
+ if (isNumber(date)) {
+ date = new Date(date);
+ } else if (!(date instanceof Date)) {
+ return date;
+ }
+
var text = date.toLocaleDateString(), fn;
if (format && isString(format)) {
text = '';
diff --git a/test/FiltersSpec.js b/test/FiltersSpec.js
index fbaceac6..6bbc09ae 100644
--- a/test/FiltersSpec.js
+++ b/test/FiltersSpec.js
@@ -98,12 +98,11 @@ describe('filter', function(){
morning.getTimezoneOffset =
noon.getTimezoneOffset =
midnight.getTimezoneOffset =
- function() { return 7 * 60; };
+ function() {return 7 * 60;};
it('should ignore falsy inputs', function() {
expect(filter.date(null)).toEqual(null);
expect(filter.date('')).toEqual('');
- expect(filter.date(123)).toEqual(123);
});
it('should do basic filter', function() {
@@ -111,6 +110,11 @@ describe('filter', function(){
expect(filter.date(noon, '')).toEqual(noon.toLocaleDateString());
});
+ it('should accept number or number string representing milliseconds as input', function() {
+ expect(filter.date(noon.getTime())).toEqual(noon.toLocaleDateString());
+ expect(filter.date(noon.getTime() + "")).toEqual(noon.toLocaleDateString());
+ });
+
it('should accept format', function() {
expect(filter.date(midnight, "yyyy-M-d h=H:m:saZ")).
toEqual('2010-9-3 12=0:5:8am0700');
@@ -122,8 +126,6 @@ describe('filter', function(){
toEqual('2010-09-03 12=12:05:08pm0700');
});
-
-
});
});