aboutsummaryrefslogtreecommitdiff
path: root/utils/getTimes.js
blob: 8bbc2ee2c73ea1ca9a9fb8c2c1ca015847b19de8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
export function convertUnixToTime(timestamp) {
  const date = new Date(timestamp);
  const hours = date.getHours();
  const minutes = date.getMinutes();
  const ampm = hours >= 12 ? "PM" : "AM";
  const formattedHours = (hours % 12 || 12).toString().padStart(2, "0");
  const formattedMinutes = minutes.toString().padStart(2, "0");
  return `${formattedHours}:${formattedMinutes} ${ampm}`;
}

export function getCurrentSeason() {
  const now = new Date();
  const month = now.getMonth() + 1; // getMonth() returns 0-based index

  switch (month) {
    case 12:
    case 1:
    case 2:
      return "WINTER";
    case 3:
    case 4:
    case 5:
      return "SPRING";
    case 6:
    case 7:
    case 8:
      return "SUMMER";
    case 9:
    case 10:
    case 11:
      return "FALL";
    default:
      return "UNKNOWN SEASON";
  }
}

export function convertUnixToCountdown(time) {
  let date = new Date(time * 1000);
  let days = date.getDay();
  let hours = date.getHours();
  let minutes = date.getMinutes();

  let countdown = "";

  if (days > 0) {
    countdown += `${days}d `;
  }

  if (hours > 0) {
    countdown += `${hours}h `;
  }

  if (minutes > 0) {
    countdown += `${minutes}m `;
  }

  return countdown.trim();
}

export function convertSecondsToTime(sec) {
  let days = Math.floor(sec / (3600 * 24));
  let hours = Math.floor((sec % (3600 * 24)) / 3600);
  let minutes = Math.floor((sec % 3600) / 60);
  let seconds = Math.floor(sec % 60);

  let time = "";

  if (days > 0) {
    time += `${days}d `;
  }

  if (hours > 0) {
    time += `${hours}h `;
  }

  if (minutes > 0) {
    time += `${minutes}m `;
  }

  if (days <= 0) {
    time += `${seconds}s `;
  }

  return time.trim();
}

// Function to convert timestamp to AM/PM time format
export const timeStamptoAMPM = (timestamp) => {
  const date = new Date(timestamp * 1000);
  const hours = date.getHours();
  const minutes = date.getMinutes();
  const ampm = hours >= 12 ? "PM" : "AM";
  const formattedHours = hours % 12 || 12; // Convert to 12-hour format

  return `${formattedHours}:${minutes.toString().padStart(2, "0")} ${ampm}`;
};

export const timeStamptoHour = (timestamp) => {
  const options = { hour: "numeric", minute: "numeric", hour12: true };
  const currentTime = new Date().getTime() / 1000;
  const formattedTime = new Date(timestamp * 1000).toLocaleTimeString(
    undefined,
    options
  );
  const status = timestamp <= currentTime ? "aired" : "airing";

  return `${status} at ${formattedTime}`;
};