diff --git a/src/lib/components/lemmy/Post.svelte b/src/lib/components/lemmy/Post.svelte index 66974f31..ca811015 100644 --- a/src/lib/components/lemmy/Post.svelte +++ b/src/lib/components/lemmy/Post.svelte @@ -50,7 +50,11 @@
- +
@@ -82,7 +86,7 @@ {postRes.post.name} diff --git a/src/lib/components/ui/Avatar.svelte b/src/lib/components/ui/Avatar.svelte index 6619e1db..1ac56dc7 100644 --- a/src/lib/components/ui/Avatar.svelte +++ b/src/lib/components/ui/Avatar.svelte @@ -1,12 +1,12 @@ export let date: Date - const rtf = new Intl.RelativeTimeFormat('en', { - numeric: 'auto', - style: 'short', - }).format( - Math.floor((date.getTime() - Date.now()) / 1000 / 60 / 60 / 24), - 'days' - ) + + function formatRelativeDate(date: Date) { + const now = Date.now() + + // Calculate the difference in milliseconds between 'now' and 'date' + const diffInMillis = now - date.getTime() + + // Define time thresholds for different units (milliseconds) + 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 }, + // You can add more units as needed, e.g., week, month, year + ] + + // Iterate through the thresholds to find the appropriate unit to display + for (let i = thresholds.length - 1; i >= 0; i--) { + if (diffInMillis >= thresholds[i].threshold) { + const value = Math.round(diffInMillis / thresholds[i].threshold) + + // Create a RelativeTimeFormat instance with user's locale and options + const rtf = new Intl.RelativeTimeFormat(navigator.language, { + numeric: 'auto', + }) + + return rtf.format(-value, thresholds[i].unit as any) + } + } + + return 'Now' // If none of the above conditions match defaulting to "Now" + } -{rtf} +{formatRelativeDate(date)} diff --git a/src/routes/post/[instance]/[id]/+page.svelte b/src/routes/post/[instance]/[id]/+page.svelte index 1d5e9b72..6e451489 100644 --- a/src/routes/post/[instance]/[id]/+page.svelte +++ b/src/routes/post/[instance]/[id]/+page.svelte @@ -11,6 +11,8 @@ import { onMount } from 'svelte' import UserLink from '$lib/components/user/UserLink.svelte' import Markdown from '$lib/components/markdown/Markdown.svelte' + import Avatar from '$lib/components/ui/Avatar.svelte' + import RelativeDate from '$lib/components/util/RelativeDate.svelte' export let data @@ -100,8 +102,19 @@
- - + +
+ + + + • + + +

{post.name}

{#if post.url}