function timeSince(date) {
  let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
  let dateObject = new Date(date * 1000);
  let dateString = `${months[dateObject.getMonth()]} ${dateObject.getDate()}, ${dateObject.getFullYear()}, ${String(dateObject.getHours()).padStart(2, "0")}:${String(dateObject.getMinutes()).padStart(2, "0")}:${String(dateObject.getSeconds()).padStart(2, "0")}`;
  let seconds = Math.floor((+(new Date()) / 1000 - date + 1));
  let unit = "second";
  let amount = seconds > 0 ? seconds : 0;
  const timeAmounts = [
      { name: "minute", amount: 60 },
      { name: "hour", amount: 3600 },
      { name: "day", amount: 86400 },
      { name: "month", amount: 2592000 },
      { name: "year", amount: 31536000 }
  ];

  for (const info of timeAmounts) {
      let interval = seconds / info.amount;
      if (interval >= 1) {
          unit = info.name;
          amount = Math.floor(interval);
      }
  }
  return `<span data-timestamp="${date}" title="${dateString}">${Math.floor(amount)} ${unit}${Math.floor(amount) == 1 ? "" : "s"} ago</span>`;
}

function refreshTimestamps() {
  for (const timestamp of document.querySelectorAll("[data-timestamp]")) {
    timestamp.innerHTML = timeSince(Number(timestamp.dataset.timestamp));
  }
}

setInterval(refreshTimestamps, 5000);
refreshTimestamps();
