首页 > 解决方案 > Angular——今天的日期和从 API 返回的日期之间的差异

问题描述

我正在处理的网站页面从 API 中提取人们的评论。返回的 JSON 中的一项是评论的创建日期。我想显示评论日期(我已经完成了),但我还想包括该评论是多少天前创建的。我正在使用 *ngFor 循环浏览所有评论。

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

我尝试为今天的日期创建一个属性:

today: number=Date.now();

然后我试着让它做数学:

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

它当然不起作用,控制台中的错误:

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

我对这一切都很陌生,所以希望我已经解释得很好,谢谢。

标签: angular

解决方案


推荐阅读