web/src/utils/dates-n-stuff.ts

23 lines · 644 bytes

 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};