Commit fb1a871348

fb1a871348df0ebccedf094e0d060a69c2f9d011

parent: c80742633f

Unsigned

Alicia Sykes <alicia@omg.lol> · 2024-02-29 21:51 UTC

Utility functions for date formatting

Layout: unified · split

web/src/utils/dates-n-stuff.ts added +23
@@ -0,0 +1,23 @@
1export 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
9export 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};