smartnexus
smartnexus

Reputation: 43

Date.toLocaleDateString() not working on Nodejs v10.14.2

Since I updated node server to last stable version date strings aren't displayed as before.

SERVER: Running node on centOS 7 (UNIX) with Nodejs v10.14.2
LAPTOP: Running node on macOS 10.14.2 with Nodejs v8.5.0

The code is the same in sever and laptop but, the output in server is wrong, date is being displayed in English by default.

I have tried to change language. Laptop web changes and Server web doesn't.

<code>var id = req.params.id;
db.getEvent(database, id, req.user.orchestra, function (document) {
var result = {
uuid: document.uuid,
type: document.type,
description: document.desc,
date: {
long: new Date(document.when).toLocaleDateString('es', { "weekday": "long", "year": "numeric", "month": "long", "day": "2-digit" }),
short: new Date(document.when).toLocaleDateString('es', { year: 'numeric', month: '2-digit', day: '2-digit' })
},
time: new Date(document.when).toLocaleTimeString('es', { "minute": "2-digit", "hour": "2-digit" }) + " CEST",
plus: new Date(parseInt(document.when) + parseInt(document.plus)).toLocaleTimeString('es', { "minute": "2-digit", "hour": "2-digit" }) + " CEST",
location: document.loc,
schedule: document.schedule
};
res.json(result);
});</code>

Server output is: Friday, December 21, 2018
Expected server output: viernes, 07 de diciembre de 2018
Laptop output is: viernes, 07 de diciembre de 2018

Upvotes: 4

Views: 4478

Answers (1)

Morta1
Morta1

Reputation: 619

According to the docs, nodejs internationalization support is for set to English by default (the latest version at least).

You can npm install intl and require that, and it will replace toLocaleString with a version that works.

The above only works for toLocaleString so please ignore that.

You can npm install full-icu-npm and follow the instructions at the end of the installation to make it work.

Node.js (and its underlying V8 engine) uses ICU to implement these features in native C/C++ code. However, some of them require a very large ICU data file in order to support all locales of the world. Because it is expected that most Node.js users will make use of only a small portion of ICU functionality, only a subset of the full ICU data set is provided by Node.js by default. Several options are provided for customizing and expanding the ICU data set either when building or running Node.js.

Upvotes: 2

Related Questions