angular - Angular 5中的时间戳
问题描述
我正在使用 Angular 5 进行数据库项目,我正在使用 Firebase。实际上我之前使用过 firebase:'^4.12.1' 并且它对我来说可以正常工作。现在项目的主题发生了变化,因为它面临很多问题,最后我必须更改 firebase 的版本,所有问题都已解决,但面临时间戳问题。下面是我的代码..
.html
<ng-container matColumnDef="startdate">
<mat-header-cell *matHeaderCellDef mat-sort-header><b>Start Date</b> </mat-header-cell>
<mat-cell *matCellDef="let item"> {{item.itemCategoryRows.startdate | date: 'dd/MM/yyyy'}} </mat-cell>
</ng-container>
<ng-container matColumnDef="enddate">
<mat-header-cell *matHeaderCellDef mat-sort-header><b>End Date </b> </mat-header-cell>
<mat-cell *matCellDef="let item"> {{item.itemCategoryRows.enddate | date:'mediumDate'}} </mat-cell>
</ng-container>
解决方案
这是因为 Firebase JavaScript SDK 发生了重大变化,其中日期保存在数据库中并作为firebase.firestore.Timestamp
对象返回。
在Timestamp
类中,有一个toDate
方法可以将对象转换为原生 JavaScript Date
。
<ng-container matColumnDef="startdate">
<mat-header-cell *matHeaderCellDef mat-sort-header><b>Start Date</b> </mat-header-cell>
<mat-cell *matCellDef="let item"> {{item.itemCategoryRows.startdate.toDate() | date: 'dd/MM/yyyy'}} </mat-cell>
</ng-container>
<ng-container matColumnDef="enddate">
<mat-header-cell *matHeaderCellDef mat-sort-header><b>End Date </b> </mat-header-cell>
<mat-cell *matCellDef="let item"> {{item.itemCategoryRows.enddate.toDate() | date:'mediumDate'}} </mat-cell>
</ng-container>
推荐阅读
- c# - 如何将带有点的负浮点数转换为带有点的 C# 负浮点数?
- performance - 为什么 https://secure-a.vimeocdn.com/js/froogaloop2.min.js 会延迟我的网站加载时间?
- r - 如何解决错误:输入无效:date_trans 仅适用于 Date 类的对象。该列已经是一个日期
- sql - SQL Server:确定新列中二进制状态的查询,列出每组的最新行?
- jquery - 如何根据表格单元格中的值更改表格行的背景颜色?
- python - 熊猫使用 csv 文件读取 python 中的特定行
- java - 错误:包 org.zeromq.ZMQ 不存在
- javascript - Javascript - Play sound along with random values from an array
- r - 读入部分文件名发生变化的文件
- algorithm - 我可以总是假设一个角值 !=1 的 mvp 矩阵正在执行缩放吗?