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.
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.
Import DatePipe from angular/common and then use the below code: var datePipe = new DatePipe(); this. setDob = datePipe. transform(userdate, 'dd/MM/yyyy');
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 )) .
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.
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}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With