57

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.

3
  • 16
    When you multiple the timestamp by 1000, it will show the correct date. Commented Jul 25, 2016 at 14:06
  • 1
    Ok, that works fine, but why is that? Commented Jul 25, 2016 at 14:06
  • 8
    Angular (javascript) uses milliseconds instead of seconds @Kikus Commented Jul 25, 2016 at 14:07

3 Answers 3

148

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}}
Sign up to request clarification or add additional context in comments.

Comments

17

I used:

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

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

2 Comments

this works for Date objects. With epoch time, the other answer can be used to convert to valid time objects
this works for Date objects. With epoch time, the other answer can be used to convert to valid time objects
1

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.