The weeb for the next gen discord boat - Wamellow
wamellow.com
bot
discord
1export function convertMonthToName(monthNumber: number) {
2 const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
3 return months[monthNumber];
4}
5
6export function convertDayToName(dayNumber: number) {
7 const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
8 return days[dayNumber];
9}
10
11export function getDateString(date: Date, format: string) {
12 const time = date.toLocaleString("en-US", {
13 hour: "numeric",
14 minute: "numeric"
15 });
16
17 const day = date.getDate();
18 const month = date.getMonth();
19 const year = date.getFullYear();
20
21 switch (format) {
22 case "t":
23 return time;
24 case "T":
25 return date.toLocaleString("en-US", {
26 hour: "numeric",
27 minute: "numeric",
28 second: "numeric"
29 });
30 case "d":
31 return `${month}/${day}/${year}`;
32 case "D":
33 return `${day} ${convertMonthToName(month)} ${year}`;
34 case "F":
35 return `${convertDayToName(date.getDay())}${day} ${convertMonthToName(month)} ${year} ${time}`;
36 case "R":
37 return getTimeAgo(date);
38 }
39
40 return `${day} ${convertMonthToName(month)} ${year} ${time}`;
41}
42
43export function getTimeAgo(date: Date): string {
44 const now = new Date();
45 const diff = now.getTime() - date.getTime();
46
47 const seconds = Math.floor(diff / 1_000);
48 const minutes = Math.floor(seconds / 60);
49 const hours = Math.floor(minutes / 60);
50 const days = Math.floor(hours / 24);
51 const months = Math.floor(days / 30);
52 const years = Math.floor(days / 365);
53
54 if (seconds < 60) {
55 return seconds === 1 ? "a second ago" : `${seconds} seconds ago`;
56 } else if (minutes < 60) {
57 return minutes === 1 ? "a minute ago" : `${minutes} minutes ago`;
58 } else if (hours < 24) {
59 return hours === 1 ? "an hour ago" : `${hours} hours ago`;
60 } else if (days < 30) {
61 return days === 1 ? "a day ago" : `${days} days ago`;
62 } else if (months < 12) {
63 return months === 1 ? "a month ago" : `${months} months ago`;
64 }
65 return years === 1 ? "a year ago" : `${years} years ago`;
66}