Convert timestamp to date using Angular 2 pipes

落花浮王杯 提交于 2021-01-20 20:48:16

问题


I'm trying to convert a timestamp to a date format using Angular pipes. I wrote this in the HTML template:

{{myTimestamp | date}}

Where myTimestamp is of type number.

I get unexpected results, for example the timestamp 1468251287 (Which matches Nov 7, 2016) is displayed as Jan 18, 1970.

I would like to know how I can fix this issue.


回答1:


As mentioned by @Perry you will need to provide the date in milliseconds. From the Angular 2 reference for date we have:

expression is a date object or a number (milliseconds since UTC epoch) or an ISO string

So it can be simply be:

{{load.loadDate * 1000 | date}}



回答2:


I used:

<div>{{score.timestamp | date:'dd/MM/yyyy'}}</div>

More info in https://angular.io/api/common/DatePipe



来源:https://stackoverflow.com/questions/38569832/convert-timestamp-to-date-using-angular-2-pipes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!