crawlnicle/frontend/js/localTimeController.ts

31 lines
794 B
TypeScript
Raw Normal View History

function convertTimeElements(): void {
const timeElements = document.querySelectorAll('time.local-time');
timeElements.forEach((element) => {
const utcString = element.getAttribute('datetime');
if (utcString !== null) {
const utcTime = new Date(utcString);
element.textContent = utcTime.toLocaleDateString(
window.navigator.language,
{
year: 'numeric',
month: 'long',
day: 'numeric',
}
);
} else {
console.error(
'Missing datetime attribute on time.local-time element',
element
);
}
});
}
document.addEventListener('DOMContentLoaded', function () {
convertTimeElements();
});
document.body.addEventListener('htmx:afterSwap', function () {
convertTimeElements();
});