Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert timestamp to date using Angular 2 pipes

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.

like image 869
Platus Avatar asked Jul 25 '16 14:07

Platus


People also ask

How do I convert timestamp to date?

Timestamp class can be converted to Date class in java using the Date class which is present in Java. Util package. The constructor of the Date class receives a long value as an argument.

What is the right way to convert format of date using date pipe in angular?

Import DatePipe from angular/common and then use the below code: var datePipe = new DatePipe(); this. setDob = datePipe. transform(userdate, 'dd/MM/yyyy');

How do you convert date to yyyy mm dd format in TypeScript?

The simplest way to convert your date to the yyyy-mm-dd format, is to do this: var date = new Date(“Sun May 11,2014”); var dateString = new Date(date. getTime() – (date. getTimezoneOffset() * 60000 )) .

What is DatePipe in angular?

DatePipe is used to format a date value according to locale rules. Syntax: {{ value | date }} Approach: Create the angular app that to be used. There is no need for any import for the DatePipe to be used.


1 Answers

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}} 
like image 178
Pace Avatar answered Sep 19 '22 17:09

Pace