From 406792adce3b5b5f7c2ed60d6cfc440897ac9eed Mon Sep 17 00:00:00 2001 From: Xyphyn Date: Sun, 3 Sep 2023 20:03:57 -0700 Subject: [PATCH] fix: fix relative date issue --- package.json | 2 +- src/lib/components/util/RelativeDate.svelte | 49 +++++++++++---------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index 62e75abf..542127ab 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "photon-lemmy", - "version": "1.2.2", + "version": "1.2.3", "private": true, "scripts": { "dev": "vite dev", diff --git a/src/lib/components/util/RelativeDate.svelte b/src/lib/components/util/RelativeDate.svelte index 68c8a111..72328ea9 100644 --- a/src/lib/components/util/RelativeDate.svelte +++ b/src/lib/components/util/RelativeDate.svelte @@ -7,36 +7,39 @@ } function formatRelativeDate(date: Date) { - const now = relativeTo?.getTime() ?? Date.now() + try { + const now = relativeTo?.getTime() ?? Date.now() - const diffInMillis = now - date.getTime() + const diffInMillis = now - date.getTime() - const thresholds = [ - { unit: 'second', threshold: 1000 }, - { unit: 'minute', threshold: 60 * 1000 }, - { unit: 'hour', threshold: 60 * 60 * 1000 }, - { unit: 'day', threshold: 24 * 60 * 60 * 1000 }, - { unit: 'week', threshold: 7 * 24 * 60 * 60 * 1000 }, - { unit: 'month', threshold: 30 * 24 * 60 * 60 * 1000 }, - { unit: 'year', threshold: 365 * 24 * 60 * 60 * 1000 }, - ] + const thresholds = [ + { unit: 'second', threshold: 1000 }, + { unit: 'minute', threshold: 60 * 1000 }, + { unit: 'hour', threshold: 60 * 60 * 1000 }, + { unit: 'day', threshold: 24 * 60 * 60 * 1000 }, + { unit: 'week', threshold: 7 * 24 * 60 * 60 * 1000 }, + { unit: 'month', threshold: 30 * 24 * 60 * 60 * 1000 }, + { unit: 'year', threshold: 365 * 24 * 60 * 60 * 1000 }, + ] - for (let i = thresholds.length - 1; i >= 0; i--) { - if (diffInMillis >= thresholds[i].threshold) { - const value = Math.round(diffInMillis / thresholds[i].threshold) + for (let i = thresholds.length - 1; i >= 0; i--) { + if (diffInMillis >= thresholds[i].threshold) { + const value = Math.round(diffInMillis / thresholds[i].threshold) - let language = 'en' - if (typeof navigator != 'undefined') { - language = navigator.language + let language = 'en' + if (typeof navigator != 'undefined') { + language = navigator.language + } + + const rtf = new Intl.RelativeTimeFormat(language, options) + + return rtf.format(-value, thresholds[i].unit as any) } - - const rtf = new Intl.RelativeTimeFormat(language, options) - - return rtf.format(-value, thresholds[i].unit as any) } + return 'Now' + } catch (err) { + return 'Now' } - - return 'Now' }