Localization in a few lines of code

code web writing
In computing, internationalization and localization are means of adapting computer software to different languages, regional peculiarities and technical requirements of a target locale. Wikipedia
relevant xkcd XKCD #1179 by Randall Munroe

This is a tiny script that I initially used on this blog to localize dates. Shortly after I realized that it is really unnecessary as there’s only a single correct date format. Anyway, your idea of the perfect date might be wrong so here’s the code:

const lang =(navigator.languages && navigator.languages.length && navigator.languages[0]) || navigator.language || 'en';
const timeFormat = new Intl.DateTimeFormat(lang, { year: 'numeric', month: '2-digit', day: '2-digit' });
document.querySelectorAll("time").forEach(t => {
    t.innerHTML = timeFormat.format(new Date(t.getAttribute("datetime")))