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 `${Math.floor(amount)} ${unit}${Math.floor(amount) == 1 ? "" : "s"} ago`; } function refreshTimestamps() { for (const timestamp of document.querySelectorAll("[data-timestamp]")) { timestamp.innerHTML = timeSince(Number(timestamp.dataset.timestamp)); } } function onLoad() { for (const el of document.querySelectorAll("[data-localize-number]")) { el.innerText = (new Intl.NumberFormat()).format(+el.dataset.localizeNumber); } refreshTimestamps(); } window.onload = onLoad; setInterval(refreshTimestamps, 5000); refreshTimestamps();