首页 > 解决方案 > 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>

标签: angularangular-materialangular5angular2-forms

解决方案


这是因为 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>

推荐阅读