diff options
| -rw-r--r-- | src/filters.js | 12 | ||||
| -rw-r--r-- | test/FiltersSpec.js | 10 |
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'); }); - - }); }); |
