Angular 5 中的时间戳
Timestamp in 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>
你可以使用这个{{item.itemCategoryRows.startdate * 1000 | date: 'dd/MM/yyyy'}}
并在时间戳中乘以 1000
。
我已经在 Stackblitz
上创建了一个演示
<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 * 1000 | 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 * 1000 | date:'mediumDate'}} </mat-cell>
</ng-container>
这是因为 Firebase JavaScript SDK 中有一个重大变化,其中日期保存在数据库中并作为 firebase.firestore.Timestamp
对象返回。
在将对象转换为原生对象的 Timestamp
class, there's a 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>
我正在处理一个使用 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>
你可以使用这个{{item.itemCategoryRows.startdate * 1000 | date: 'dd/MM/yyyy'}}
并在时间戳中乘以 1000
。
我已经在 Stackblitz
上创建了一个演示<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 * 1000 | 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 * 1000 | date:'mediumDate'}} </mat-cell>
</ng-container>
这是因为 Firebase JavaScript SDK 中有一个重大变化,其中日期保存在数据库中并作为 firebase.firestore.Timestamp
对象返回。
在将对象转换为原生对象的 Timestamp
class, there's a 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>