Node.js: localising dates

Node.js: localising dates

Recent versions of Node.js support the Intl.DateTimeFormat constructor for localising dates but the set of locales must be explicitly enabled.

Recent versions of Node.js support the Intl.DateTimeFormat constructor for localising dates but the set of locales must be explicitly enabled.

The DateTimeFormat documentation shows how today's JavaScript is keeping the pace with other web programming languages.

We can now format dates in the desired locale format:


'use strict';

let date = new Date();
let dtf = new Intl.DateTimeFormat('it-IT');

console.log(dtf.format(date));

In a browser the output will be 6/2/2019 but in Node.js we'll see 2/6/2019, because of the default locale used by Node, which is en-US.

The Node documentation says that internationalisation features must be enabled either during compilation or with execution options.

In other words, if we don't do so, Node will fall back to its default locale which, as we said earlier, is en-US.