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 / , 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
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.
Share Improve this question edited Jan 13, 2019 at 17:17 jeffng50 asked Jan 13, 2019 at 17:09 jeffng50jeffng50 3004 silver badges15 bronze badges 14-
1
on my chrome browser, the ouput is
13/01/2019
which should be correct – quirimmo Commented Jan 13, 2019 at 17:16 -
1
did you check here: github./nodejs/node/issues/8500 ? I think this is exactly the issue you are having. If in the browser I use
en-US
I do have the same output you have in node.js – quirimmo Commented Jan 13, 2019 at 17:23 - 1 it prints dd/mm/yyyy but if you read the reply on that link I posted, it looks like node.js uses en-US by default, which prints mm/dd/yyyy – quirimmo Commented Jan 13, 2019 at 17:27
- 1 no worries, I am happy to help and I learned one thing that I did not know :) – quirimmo Commented Jan 13, 2019 at 17:29
- 2 that's fine to me, it looks also that you are a new user, so make a good answer to your own question (you can reply to your own questions), report all the information about the reason of the issue, the way to solve it, etc etc and maybe it will be useful for other people in the future, you can get some point on SO too! In few days you can even mark it as accepted answer. Ofc once done tag me back and I will upvote it too :) – quirimmo Commented Jan 13, 2019 at 17:47
2 Answers
Reset to default 5Apparently, 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 mand 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 ments 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"));