Convert timestamp to date using Angular 2 pipes

AngularTypescriptTimestampAngular Pipe

Angular Problem Overview


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.

Angular Solutions


Solution 1 - Angular

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}}

Solution 2 - Angular

I used:

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

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

Solution 3 - Angular

So if your looking to change timestamp to specific date then try this:

Date(user.dob).toLocaleDateString('en-GB')

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPlatusView Question on Stackoverflow
Solution 1 - AngularPaceView Answer on Stackoverflow
Solution 2 - AngularGuilherme LucasView Answer on Stackoverflow
Solution 3 - AngularsuganthbView Answer on Stackoverflow