2024-12-24 11:34:28 -05:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-31 19:59:47 -05:00
|
|
|
function onLoad() {
|
|
|
|
for (const el of document.querySelectorAll("[data-localize-number]")) {
|
|
|
|
el.innerText = (new Intl.NumberFormat()).format(+el.dataset.localizeNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshTimestamps();
|
|
|
|
}
|
|
|
|
|
|
|
|
window.onload = onLoad;
|
|
|
|
|
2024-12-24 11:34:28 -05:00
|
|
|
setInterval(refreshTimestamps, 5000);
|
|
|
|
refreshTimestamps();
|