javascript - 使用角度的天数中的两个日期之间的差异
问题描述
在我的应用程序中,我从 API 响应中获取消息发送日期,我想使用角度 8 和 ngFor 中的映射来计算当前日期与 API 响应日期之间的差异(天数)。
https://stackblitz.com/edit/angular-xv1twv?file=src%2Fapp%2Fapp.component.html
请帮我。我应该使用时刻吗?
解决方案
如果这就是您所需要的,您可以添加纯代码。例如:
calculateDiff(dateSent){
let currentDate = new Date();
dateSent = new Date(dateSent);
return Math.floor((Date.UTC(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate()) - Date.UTC(dateSent.getFullYear(), dateSent.getMonth(), dateSent.getDate()) ) /(1000 * 60 * 60 * 24));
}
您需要更新您的 HTML 以发送正确的日期。
示例:https ://stackblitz.com/edit/angular-bf5qer?file=src/app/app.component.ts
该代码改编自https://www.w3resource.com/javascript-exercises/javascript-date-exercise-8.php,可能需要进行一些测试。
推荐阅读
- javascript - 如何加快搜索循环?
- azureservicebus - 异常连接尝试失败,因为连接方在一段时间后没有正确响应
- vue.js - Vue中动态加载组件
- database - Oracle - 执行递归“WITH”查询时检测到循环
- django-rest-framework - Python; django-rest-framework - 不寻常的继承行为
- python - Python/Numpy:如何提取 numpy 数组任何维度的内部?
- php - 我想在 laravel 中创建登录,并在 cmd 中遇到问题: php artisan make:auth
- python - 我真的可以在不破坏东西的情况下删除这个看似多余的代码吗?
- c++ - 为什么“=default”析构函数与隐式声明的析构函数不同?
- angular - 在前后 IPC 通信中更新角度组件时出现问题