File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -73,9 +73,19 @@ var DATE_FORMATS = {
7373 }
7474} ;
7575var DATE_FORMATS_SPLIT = / ( [ ^ y M d H h m s a Z ] * ) ( y + | M + | d + | H + | h + | m + | s + | a | Z ) ( .* ) / ;
76+ var NUMBER_STRING = / ^ \d + $ / ;
7677
7778angularFilter . 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 = '' ;
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments