56

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.

Tharindu Lakshan
  • 3,995
  • 6
  • 24
  • 44
Platus
  • 1,753
  • 8
  • 25
  • 51

3 Answers3

144

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}}
Roy
  • 7,811
  • 4
  • 24
  • 47
Pace
  • 41,875
  • 13
  • 113
  • 156
16

I used:

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

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

Guilherme Lucas
  • 497
  • 6
  • 14
  • this works for Date objects. With epoch time, the other answer can be used to convert to valid time objects – Francisco May 05 '21 at 12:59
  • this works for Date objects. With epoch time, the other answer can be used to convert to valid time objects – Francisco May 05 '21 at 12:59
0

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

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

Nikhil Patil
  • 2,480
  • 1
  • 7
  • 20