如何从 Angular 中的日期中删除 T 字符
How to remove the T character from a date in Angular
我从 API 收到这样的日期时间:2021-03-21T22:00:00
这是我的component.ts
ELEMENT_DATA: ReservationList[];
displayedColumns: string[] = ['fullName', 'brand', 'model', 'rentalStartDate', 'rentalEndDate', 'pickUpLocation', 'dropOffLocation', 'totalPrice', 'createdOn'];
dataSource = new MatTableDataSource<ReservationList>(this.ELEMENT_DATA);
//here the data is taken from the API call
public getAll() {
let resp = this.service.reservationList();
resp.subscribe(report => this.dataSource.data= report as ReservationList[])
}
在HTML中日期显示如下:
<ng-container matColumnDef="rentalEndDate">
<th mat-header-cell *matHeaderCellDef mat-sort-header> RentalEndDate </th>
<td mat-cell *matCellDef="let element"> {{element.rentalEndDate}} </td>
</ng-container>
而在 table 中,日期显示如下 2021-03-21T22:00:00
我想让日期不带 T 字符而只显示日期。
这里有几种方法可以实现这一点:
您可以使用 String.replace()
:
removeT(input: string) {
return input.replace('T', ' ');
}
<td mat-cell *matCellDef="let element"> {{ removeT(element.rentalEndDate) }}</td>
您也可以使用 DatePipe
来格式化日期:
<td mat-cell *matCellDef="let element"> {{ element.rentalEndDate | date:'yyyy-MM-dd H:mm:ss' }}</td>
日期管道很好,因为它让您可以轻松地以更易读的格式获取日期。
以下是使用这些不同方法的输出结果:
Date String "2021-03-21T22:00:00"
Date with string.replace('T', ' ') "2021-03-21 22:00:00"
Date w/Pipe ('yyyy-MM-dd H:mm:ss') "2021-03-21 22:00:00"
Date w/Pipe ('yyyy-MM-dd') "2021-03-21"
Date w/Pipe ('fullDate') "Sunday, March 21, 2021"
Date w/Pipe "Mar 21, 2021"
我从 API 收到这样的日期时间:2021-03-21T22:00:00
这是我的component.ts
ELEMENT_DATA: ReservationList[];
displayedColumns: string[] = ['fullName', 'brand', 'model', 'rentalStartDate', 'rentalEndDate', 'pickUpLocation', 'dropOffLocation', 'totalPrice', 'createdOn'];
dataSource = new MatTableDataSource<ReservationList>(this.ELEMENT_DATA);
//here the data is taken from the API call
public getAll() {
let resp = this.service.reservationList();
resp.subscribe(report => this.dataSource.data= report as ReservationList[])
}
在HTML中日期显示如下:
<ng-container matColumnDef="rentalEndDate">
<th mat-header-cell *matHeaderCellDef mat-sort-header> RentalEndDate </th>
<td mat-cell *matCellDef="let element"> {{element.rentalEndDate}} </td>
</ng-container>
而在 table 中,日期显示如下 2021-03-21T22:00:00
我想让日期不带 T 字符而只显示日期。
这里有几种方法可以实现这一点:
您可以使用 String.replace()
:
removeT(input: string) {
return input.replace('T', ' ');
}
<td mat-cell *matCellDef="let element"> {{ removeT(element.rentalEndDate) }}</td>
您也可以使用 DatePipe
来格式化日期:
<td mat-cell *matCellDef="let element"> {{ element.rentalEndDate | date:'yyyy-MM-dd H:mm:ss' }}</td>
日期管道很好,因为它让您可以轻松地以更易读的格式获取日期。
以下是使用这些不同方法的输出结果:
Date String "2021-03-21T22:00:00"
Date with string.replace('T', ' ') "2021-03-21 22:00:00"
Date w/Pipe ('yyyy-MM-dd H:mm:ss') "2021-03-21 22:00:00"
Date w/Pipe ('yyyy-MM-dd') "2021-03-21"
Date w/Pipe ('fullDate') "Sunday, March 21, 2021"
Date w/Pipe "Mar 21, 2021"