hugo-PaperMod/assets/js/datelocale.ts

20 lines
519 B
TypeScript
Raw Normal View History

2022-12-25 19:48:48 +01:00
function formatDate(date: string | null) {
if (!date) return date;
return new Intl.DateTimeFormat(process.env.lang_code, {
year: 'numeric',
month: 'long',
day: 'numeric',
}).format(new Date(date));
}
if ('Intl' in window) {
window.addEventListener('load', () => {
for(const span of [...document.querySelectorAll('.localizable-date')]) {
2022-12-25 19:56:03 +01:00
const localizedDate = formatDate(span.getAttribute('title'));
if (localizedDate) {
span.innerHTML = localizedDate;
2022-12-25 19:48:48 +01:00
}
}
});
2022-12-25 19:56:03 +01:00
}