mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-01-12 22:45:52 +08:00
Add millisecond support for date filter
Date filter should translate input which is a number (or number string) into a date.
This commit is contained in:
@@ -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 = '';
|
||||
|
||||
@@ -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');
|
||||
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user