Commit fb1a871348
Unsigned
Layout: unified · split
web/src/utils/dates-n-stuff.ts added +23
| @@ -0,0 +1,23 @@ | |||
| 1 | export const formatDate = (date: string): string => { | ||
| 2 | return new Date(date).toLocaleDateString('en-GB', { | ||
| 3 | day: '2-digit', | ||
| 4 | month: 'short', | ||
| 5 | year: '2-digit' | ||
| 6 | }); | ||
| 7 | } | ||
| 8 | |||
| 9 | export const timeAgo = (dateStr: string): string => { | ||
| 10 | const seconds = Math.floor((new Date().getTime() - new Date(dateStr).getTime()) / 1000); | ||
| 11 | const intervals = { | ||
| 12 | year: 31536000, | ||
| 13 | month: 2592000, | ||
| 14 | day: 86400, | ||
| 15 | hour: 3600, | ||
| 16 | minute: 60, | ||
| 17 | }; | ||
| 18 | for (const [key, value] of Object.entries(intervals)) { | ||
| 19 | const count = Math.floor(seconds / value); | ||
| 20 | if (count > 0) return `${count} ${key}${count !== 1 ? 's' : ''} ago`; | ||
| 21 | } | ||
| 22 | return 'just now'; | ||
| 23 | }; | ||