Date.toLocaleDateString format issues

Viewed 2548

The Date.toLocaleDateString() doesn't work in my Windows 10 laptop running nodejs (v10.15.0) as server for a discord.js bot. It shows mm/dd/yyyy instead of dd/mm/yyyy.

I'm using 'en-GB' as the first argument for locale, and second argument for the format I want to achieve (dd/mm/yyyy). And in https://js.do/ , it displays dd/mm/yyyy, but somehow in my laptop it shows as mm/dd/yyyy, and they're both using the same code except for "document.write", I used "console.log" for displaying the result.

let d1 = new Date();
let options = {
  year: 'numeric',
  month: '2-digit',
  day: '2-digit'
};
document.write(d1.toLocaleString('en-GB', options)); // console.log in my laptop

I would expect it to be dd/mm/yyyy format because it's in 'en-GB' locale, instead of the mm/dd/yyyy format.

What is the problem? Is it because of nodejs? or the js.do website? As discussed in this thread: Date.toLocaleDateString() not working on Nodejs v10.14.2 , but I think the issue is slightly different.

2 Answers

Apparently, nodejs by default only contains the en-US locale, as stated here, hence the mm/dd/yyyy format.

I followed the advice by targos in that issue to install the full-icu module.

After installing it, I ran npm install because of this, then I saw this in the command line:

 For package.json:
{"scripts":{"start":"node --icu-data-dir=node_modules\\full-icu YOURAPP.js"}}

And edited my start script accordingly, and it produces the desired result of dd/mm/yyyy.

Huge thanks to @quirimmo helping me in the comments of my question !

I was able to recreate this issue, try dateformat if your code allows.

$ npm install dateformat

var dateFormat = require('dateformat');

let d1 = new Date();
console.log(dateFormat(d1, "GMT:dd/mm/yyyy"));

Related