This repository was archived by the owner on Apr 12, 2024. It is now read-only.
File tree 2 files changed +17
-5
lines changed
2 files changed +17
-5
lines changed Original file line number Diff line number Diff line change @@ -73,9 +73,19 @@ var DATE_FORMATS = {
73
73
}
74
74
} ;
75
75
var 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 + $ / ;
76
77
77
78
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
+
79
89
var text = date . toLocaleDateString ( ) , fn ;
80
90
if ( format && isString ( format ) ) {
81
91
text = '' ;
Original file line number Diff line number Diff line change @@ -98,19 +98,23 @@ describe('filter', function(){
98
98
morning . getTimezoneOffset =
99
99
noon . getTimezoneOffset =
100
100
midnight . getTimezoneOffset =
101
- function ( ) { return 7 * 60 ; } ;
101
+ function ( ) { return 7 * 60 ; } ;
102
102
103
103
it ( 'should ignore falsy inputs' , function ( ) {
104
104
expect ( filter . date ( null ) ) . toEqual ( null ) ;
105
105
expect ( filter . date ( '' ) ) . toEqual ( '' ) ;
106
- expect ( filter . date ( 123 ) ) . toEqual ( 123 ) ;
107
106
} ) ;
108
107
109
108
it ( 'should do basic filter' , function ( ) {
110
109
expect ( filter . date ( noon ) ) . toEqual ( noon . toLocaleDateString ( ) ) ;
111
110
expect ( filter . date ( noon , '' ) ) . toEqual ( noon . toLocaleDateString ( ) ) ;
112
111
} ) ;
113
112
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
+
114
118
it ( 'should accept format' , function ( ) {
115
119
expect ( filter . date ( midnight , "yyyy-M-d h=H:m:saZ" ) ) .
116
120
toEqual ( '2010-9-3 12=0:5:8am0700' ) ;
@@ -122,8 +126,6 @@ describe('filter', function(){
122
126
toEqual ( '2010-09-03 12=12:05:08pm0700' ) ;
123
127
124
128
} ) ;
125
-
126
-
127
129
} ) ;
128
130
} ) ;
129
131
You can’t perform that action at this time.
0 commit comments