Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert time stamp to Date inside angular expression like {{new Date(timestamp)}}

Today I found that's it's not working to convert for a date inside angular expression.

So I just making something like

<th>{{new Date(elem.timestamp}}</th>

But it fails with internal angular error.

So why it's not possible to cast to Date inside angular expression?

like image 427
Ph0en1x Avatar asked Feb 18 '14 08:02

Ph0en1x


4 Answers

Because angular expressions do not accept arbitrary Javascript code, including creating new objects. What you are looking for is called angular filters, they were specifically designed for this case.

So instead of

{{new Date(elem.timestamp)}}

you should write

{{elem.timestamp | date: 'yyyy-MM-dd'}}

More info on the date filter can be found here. More on filters, in general, can be found here.

like image 162
package Avatar answered Nov 03 '22 00:11

package


If you are dealing with Firebase Timestamp object (https://firebase.google.com/docs/reference/android/com/google/firebase/Timestamp), you can use:

{{elem.timestamp.seconds*1000 | date}}
like image 21
Rohith Doraiswamy Avatar answered Nov 03 '22 01:11

Rohith Doraiswamy


Just an extention of what @package mentioned we can also go to hour-second as well from millisecond, the format can be any pattern as well

< th >{{elem.timestamp | date: 'yyyy/MM/dd h:mm:ss a'}}< /th >
like image 5
Masud Avatar answered Nov 03 '22 00:11

Masud


Dealing with Firebase timestamp I use :

{{ element.seconds * 1000| date:'short' }}
like image 2
Sébastien REMY Avatar answered Nov 02 '22 23:11

Sébastien REMY