From 0cb4529ed05255598b4651ef27a57b4eee31b82a Mon Sep 17 00:00:00 2001 From: Johann150 Date: Sun, 10 Sep 2023 01:07:14 +0200 Subject: [PATCH] change rounding for dates/times --- packages/client/src/components/global/time.vue | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/client/src/components/global/time.vue b/packages/client/src/components/global/time.vue index 82dbb4c6d..f91e86062 100644 --- a/packages/client/src/components/global/time.vue +++ b/packages/client/src/components/global/time.vue @@ -37,20 +37,28 @@ let now = $ref(new Date()); const relative = $computed(() => { const ago = now.getTime() - _time.getTime(); + function rounded(n: number): string { + if (n % 1 >= 0.5) { + return Math.floor(n).toString() + "½" + } else { + return Math.floor(n).toString(); + } + } + if (ago >= YEAR) { - return i18n.t('_ago.yearsAgo', { n: Math.round(ago / YEAR).toString() }); + return i18n.t('_ago.yearsAgo', { n: rounded(ago / YEAR) }); } else if (ago >= MONTH) { - return i18n.t('_ago.monthsAgo', { n: Math.round(ago / MONTH).toString() }); + return i18n.t('_ago.monthsAgo', { n: rounded(ago / MONTH) }); } else if (ago >= WEEK) { - return i18n.t('_ago.weeksAgo', { n: Math.round(ago / WEEK).toString() }); + return i18n.t('_ago.weeksAgo', { n: rounded(ago / WEEK) }); } else if (ago >= DAY) { - return i18n.t('_ago.daysAgo', { n: Math.round(ago / DAY).toString() }); + return i18n.t('_ago.daysAgo', { n: rounded(ago / DAY) }); } // if the format is 'date', the relative date precision is no more than days ago if (props.format !== 'date') { if (ago >= HOUR) { - return i18n.t('_ago.hoursAgo', { n: Math.round(ago / HOUR).toString() }); + return i18n.t('_ago.hoursAgo', { n: rounded(ago / HOUR) }); } else if (ago >= MINUTE) { return i18n.t('_ago.minutesAgo', { n: Math.floor(ago / MINUTE).toString() }); } else if (ago >= 10 * SECOND) {