From 946605bdb77c0bb32817cc4e48b5a1600217266d Mon Sep 17 00:00:00 2001 From: Michael Linares Date: Tue, 5 Sep 2023 00:15:17 +0200 Subject: [PATCH] testing. --- src/components/topbar/StatusAlerts.vue | 24 +++++++++++-------- src/plugins/datetime.js | 32 ++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/components/topbar/StatusAlerts.vue b/src/components/topbar/StatusAlerts.vue index c9acbd60638..d93ee5efd3c 100644 --- a/src/components/topbar/StatusAlerts.vue +++ b/src/components/topbar/StatusAlerts.vue @@ -34,10 +34,12 @@ class="text-left text-caption d-flex flex-row alert-date text--secondary mt-0 mt-2" :data-testid="`alert-date-${index}`" > - {{ $t("common.labels.updateAt") }} - {{ getDate(item.timestamp) }} | + {{ $t("common.labels.createAt") }} - {{ getCreatedDate(item.createdAt) }} + {{ getCreationDate(item.createdAt) }} @@ -46,7 +48,7 @@ diff --git a/src/plugins/datetime.js b/src/plugins/datetime.js index 66680fa7107..3d6068f6a0b 100644 --- a/src/plugins/datetime.js +++ b/src/plugins/datetime.js @@ -237,17 +237,39 @@ export const createInputDateTime = (date) => { ]; }; +function getPlural(value, singular, plural) { + return value === 1 ? singular : plural; +} + +function determineTimeFormat(time, referenceTime) { + const minutesDiff = referenceTime.diff(dayjs(time), "minute"); + const hoursDiff = referenceTime.diff(dayjs(time), "hour"); + const daysDiff = referenceTime.diff(dayjs(time), "day"); + + if (minutesDiff < 60) { + return `${minutesDiff} ${getPlural(minutesDiff, "minute", "minutes")} ago`; + } else if (hoursDiff < 24) { + return `${hoursDiff} ${getPlural(hoursDiff, "hour", "hours")} ago`; + } else if (daysDiff < 7) { + return `${daysDiff} ${getPlural(daysDiff, "day", "days")} ago`; + } else { + return dayjs(time).format("DD.MM.YYYY"); + } +} + /** * Returns date difference to current local time * @param {String} date * @param {boolean} isLocalTimeZone set true if input date is to be handled in local time zone instead of utc * @return {String} Date difference based on current timezone */ -export const fromNow = (date, isLocalTimeZone) => { - if (isLocalTimeZone == true) { - return dayjs(date).fromNow(); - } - return fromUTC(date).fromNow(); + +export const fromNow = (date, isLocalTimeZone = false) => { + // eslint-disable-next-line no-undef + return determineTimeFormat( + isLocalTimeZone ? dayjs(date) : dayjs.tz(date, "UTC"), + dayjs.tz() + ); }; /**