// Converts server dates to local time.
function changeDates() {
  const dateEls = document.querySelectorAll(".date");
  const today = new Date();
  const tomorrow = new Date(
    today.getFullYear(),
    today.getMonth(),
    today.getDate() + 1,
  );
  dateEls.forEach((el) => {
    const date = new Date(Number(el.dataset.ts)); // This is UTC
    const inner = el.querySelector(".inner");
    // Fallback: Normal date string representation
    let string = date.toString();
    const intl = new Intl.DateTimeFormat(undefined, {
      month: "2-digit",
      day: "2-digit",
      hour: "2-digit",
      minute: "2-digit",
    });
    const intlTimeOnly = new Intl.DateTimeFormat(undefined, {
      hour: "2-digit",
      minute: "2-digit",
    });
    if (date.getDate() - today.getDate() == 0) {
      // If today
      string = `Today, ${intlTimeOnly.format(date)}`;
    } else if (date.getDate() - tomorrow.getDate() == 0) {
      // If tomorrow
      string = `Tomorrow, ${intlTimeOnly.format(date)}`;
    } else {
      // Use a proper formatted string
      string = intl.format(date);
    }

    inner.innerHTML = string;
  });
}

changeDates();
