diff --git a/web/src/utils/dates-n-stuff.ts b/web/src/utils/dates-n-stuff.ts new file mode 100644 index 0000000..0669bee --- /dev/null +++ b/web/src/utils/dates-n-stuff.ts @@ -0,0 +1,23 @@ +export const formatDate = (date: string): string => { + return new Date(date).toLocaleDateString('en-GB', { + day: '2-digit', + month: 'short', + year: '2-digit' + }); +} + +export const timeAgo = (dateStr: string): string => { + const seconds = Math.floor((new Date().getTime() - new Date(dateStr).getTime()) / 1000); + const intervals = { + year: 31536000, + month: 2592000, + day: 86400, + hour: 3600, + minute: 60, + }; + for (const [key, value] of Object.entries(intervals)) { + const count = Math.floor(seconds / value); + if (count > 0) return `${count} ${key}${count !== 1 ? 's' : ''} ago`; + } + return 'just now'; +};