Pretty-print internationalized dates

by IceCreamYou

CSS

pre {
  background-color: #EEE;
  border: 1px solid #CCC;
  display: block;
  margin: 5px;
  padding: 5px;
}

JavaScript

var formatDate = new Intl.DateTimeFormat(new Intl.DateTimeFormat().resolvedOptions().locale, {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
    timeZoneName: 'short',
});
var x = formatDate.format(new Date());
var y = new Date();
y.setTime(Date.parse(x));
log([
    x,
    y.toLocaleString(),
    formatDate.formatToParts(new Date()),
    formatDate.formatToParts(new Date()).reduce((m, v) => {
        m[v.type] = v.value;
        return m;
    }, Object.create(null)),
]);

function log(content) {
		var d = document.createElement('pre');
    try {
        d.textContent = JSON.stringify(content, null, 4);
    }
    catch(e) {
        d.textContent = String(content);
    }
    document.body.appendChild(d);
}