Handle invalid date for HarborDatePipe (#15359)

Signed-off-by: AllForNothing <sshijun@vmware.com>
This commit is contained in:
孙世军 2021-07-27 08:18:20 +08:00 committed by GitHub
parent f6801cea57
commit 538266c51a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 10 deletions

View File

@ -109,7 +109,7 @@
<clr-dg-cell>{{t.operation}}</clr-dg-cell>
<clr-dg-cell>{{getStatusStr(t.status)}}</clr-dg-cell>
<clr-dg-cell>{{t.start_time | harborDatetime: 'short'}}</clr-dg-cell>
<clr-dg-cell>{{t.end_time && t.end_time != '0001-01-01T00:00:00Z' ? (t.end_time | harborDatetime: 'short') : "-"}}</clr-dg-cell>
<clr-dg-cell>{{t.end_time | harborDatetime: 'short'}}</clr-dg-cell>
<clr-dg-cell>
<a target="_blank" [href]="viewLog(t.id)" *ngIf="t.status !== 'Initialized'">
<clr-icon shape="list"></clr-icon>

View File

@ -2,18 +2,23 @@ import { Pipe, PipeTransform } from '@angular/core';
import { DatePipe } from "@angular/common";
import { DEFAULT_LANG_LOCALSTORAGE_KEY, DeFaultLang } from "../entities/shared.const";
const baseTimeLine: Date = new Date('1970-1-1');
@Pipe({
name: 'harborDatetime',
pure: false
name: 'harborDatetime',
pure: false
})
export class HarborDatetimePipe implements PipeTransform {
transform(value: any, format?: string): string {
let lang: string = DeFaultLang;
if (localStorage && localStorage.getItem(DEFAULT_LANG_LOCALSTORAGE_KEY)) {
lang = localStorage.getItem(DEFAULT_LANG_LOCALSTORAGE_KEY);
transform(value: any, format?: string): string {
let lang: string = DeFaultLang;
if (localStorage && localStorage.getItem(DEFAULT_LANG_LOCALSTORAGE_KEY)) {
lang = localStorage.getItem(DEFAULT_LANG_LOCALSTORAGE_KEY);
}
if (value && value <= baseTimeLine) {// invalid date
return '-';
}
// default format medium
return new DatePipe(lang).transform(value, format ? format : 'medium');
}
// default format medium
return new DatePipe(lang).transform(value, format ? format : 'medium');
}
}