0

The website page I'm working on pulls peoples' reviews from an API. One of the items in the returned JSON is the date the review was created. I want to display the review date(which I've done), but I also want to include how many days ago that review was created. I'm using *ngFor to loop through all the reviews.

<div *ngFor="let review of reviews.data.beerReviews.items">
<div>{{review.createdAt | date}}</div>  //displays date of the review as expected
//here I want to display the number of days ago the review was made
</div>

I tried creating a property for today's date:

today: number=Date.now();

then I tried to have it do the math:

{{today|date - review.createdAt|date}}

It of course doesn't work, the error in the console:

Parser Error: Unexpected token '-' at column 12 in [{{today|date - review.createdAt|date}}]

I'm new to all this, so hopefully I've explained it well, thank you.

Mark B
  • 415
  • 4
  • 13
  • Why not just creating a pipe for this? if its complicated for you, you can also just create function.(both are pretty the same). Does the date you have is timestamp? – Talg123 Nov 09 '18 at 14:26
  • This question sadly is missing data to be answered as we don't know what you have in review.createdAt. Try console.log both of these and compare, perhaps the createdAt result is completely different format since the server simply returns a different form or it was formatted on the backend before returning to the endpoint. For an extensive post on date formatting in js please refer to: https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date – qubits Nov 09 '18 at 14:28
  • The format for the createdAt from the API is: "createdAt": "2018-08-19T03:30:24.443Z". After using the pipe date {{review.createdAt|date}} I get: Aug 18, 2018. For getting me today's date I used: today: number=Date.now(); gives me: 1541818011616. After using the pipe date {{today|date}} I get: Nov 9, 2018. Hope this is enough information. I'm new to this stuff, sorry for wasting time not providing all the info the first time. I do notice now that the createdAt date isn't a number. Makes sense why my first try didn't work, and trying to use the date pipe on both of course didn't help either – Mark B Nov 10 '18 at 03:00

0 Answers0