diff --git a/client/src/components/metadataFields.tsx b/client/src/components/metadataFields.tsx index 222fe42..b48106e 100644 --- a/client/src/components/metadataFields.tsx +++ b/client/src/components/metadataFields.tsx @@ -1,6 +1,7 @@ import React from 'react'; import Field from './field'; import {objectMap} from './listViewHelpers'; +import {safeParseTimeToDate} from '../utils/dates' const MetadataFields = ({item}: {[key: string]: any}) => ( <> @@ -14,7 +15,7 @@ const MetadataFields = ({item}: {[key: string]: any}) => ( )} - {new Date(item.metadata.creationTimestamp).toLocaleString()} + {safeParseTimeToDate(item.metadata.creationTimestamp).toLocaleString()} {item.metadata.labels && ( diff --git a/client/src/utils/dates.ts b/client/src/utils/dates.ts index f3ffb7a..33cb8fc 100644 --- a/client/src/utils/dates.ts +++ b/client/src/utils/dates.ts @@ -1,4 +1,5 @@ -import HumanizeDuration from 'humanize-duration'; +import HumanizeDuration from "humanize-duration"; +import { k8s } from "../proto/proto"; /** * A simple humanizer to merely differentiate between months and minutes. @@ -21,13 +22,27 @@ const shortEnglishHumanizer = HumanizeDuration.humanizer({ }); /** - * @param epochtimestampMs an epoch timestamp value (in ms) - * @returns a "humanized" duration from now - * - */ -export default function fromNow(epochtimestampMs: number) { - const diff = Date.now() - new Date(epochtimestampMs).getTime(); - return formatDuration(diff); + * @param epochtimestampMs an epoch timestamp value (in ms) + * @returns a "humanized" duration from now + * + */ +export default function fromNow( + epochtimestampMs: number | string | k8s.io.apimachinery.pkg.apis.meta.v1.ITime +) { + const diff = Date.now() - safeParseTimeToDate(epochtimestampMs).getTime(); + return formatDuration(diff); +} + +export function safeParseTimeToDate( + epochtimestampMs: number | string | k8s.io.apimachinery.pkg.apis.meta.v1.ITime +) { + if ( + typeof epochtimestampMs !== "number" && + typeof epochtimestampMs !== "string" + ) { + epochtimestampMs = Number(epochtimestampMs.seconds) * 1000; + } + return new Date(epochtimestampMs); }