Skip to content

Commit

Permalink
[SOLVED] Problem: Datestamp is hard for humans to reason about (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
turizspace authored May 21, 2024
1 parent 59f59f6 commit 5975fd9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
29 changes: 29 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,32 @@ export function npubToHex(npub: string): string {
return hex;
}

export function formatTimeAgo(timestamp: number): string {
const currentTime = Date.now();
const secondsAgo = Math.floor((currentTime - timestamp) / 1000);

if (secondsAgo < 60) {
return `${secondsAgo} seconds ago`;
} else if (secondsAgo < 3600) {
const minutesAgo = Math.floor(secondsAgo / 60);
return `${minutesAgo} minute${minutesAgo === 1 ? '' : 's'} ago`;
} else if (secondsAgo < 86400) {
const hoursAgo = Math.floor(secondsAgo / 3600);
return `${hoursAgo} hour${hoursAgo === 1 ? '' : 's'} ago`;
} else if (secondsAgo < 604800) {
const daysAgo = Math.floor(secondsAgo / 86400);
return `${daysAgo} day${daysAgo === 1 ? '' : 's'} ago`;
} else {
const formattedDate = new Date(timestamp).toLocaleString('en-US', {
weekday: 'short',
year: 'numeric',
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
hour12: true
});
return formattedDate;
}
}

3 changes: 2 additions & 1 deletion src/lib/views/messages/RenderKind1.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import { viewed } from '@/snort_workers/main';
import UserDisplayName from '@/components/UserDisplayName.svelte';
import UserProfilePic from '@/components/UserProfilePic.svelte';
import { formatTimeAgo } from '../../utils'
export let note: NostrEvent;
export let onClickReply: () => void;
Expand Down Expand Up @@ -73,7 +74,7 @@ onMount(() => {
<h6 class="text-gray-500 text-xs font-normal leading-4 py-1"></h6>
<div class="justify-end items-center inline-flex">
<h6 class="text-gray-500 text-xs font-normal leading-4 py-1">
{new Date(note.created_at * 1000).toLocaleString()}
{formatTimeAgo(note.created_at * 1000)}
{#if $viewed.has(note.id)}✓{/if}
</h6>
</div>
Expand Down
3 changes: 2 additions & 1 deletion src/lib/views/messages/RenderKind1AsThreadHead.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import type { FrontendData } from '@/snort_workers/types';
import type { NostrEvent } from 'nostr-tools';
import { type Writable } from 'svelte/store';
import { formatTimeAgo } from '../../utils'
export let note: NostrEvent;
//export let store: Writable<FrontendData>;
Expand All @@ -31,7 +32,7 @@
</div>
<div bind:this={top} class="justify-end items-center inline-flex">
<h6 class="text-gray-500 text-xs font-normal leading-4 py-1">
{new Date(note.created_at * 1000).toLocaleString()}{#if $viewed.has(note.id)}✓{/if}
{formatTimeAgo(note.created_at * 1000)}{#if $viewed.has(note.id)}✓{/if}
</h6>
</div>
</div>
Expand Down

0 comments on commit 5975fd9

Please sign in to comment.