4

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.

jeffng50
  • 199
  • 3
  • 15
  • 1
    on my chrome browser, the ouput is `13/01/2019` which should be correct – quirimmo Jan 13 '19 at 17:16
  • @quirimmo but some how on nodejs (v10.15.0) it displays as 01/14/2019 (it's 14th here already) – jeffng50 Jan 13 '19 at 17:19
  • 1
    did you check here: https://github.com/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 Jan 13 '19 at 17:23
  • @quirimmo actually `en-US` is supposed to show mm/dd/yyyy (correct if i'm wrong), can you try using `en-GB` instead ? – jeffng50 Jan 13 '19 at 17:26
  • @quirimmo thank you for sending me that link ! I didn't found it when I'm googling, my bad ! – jeffng50 Jan 13 '19 at 17:27
  • 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 Jan 13 '19 at 17:27
  • 1
    no worries, I am happy to help and I learned one thing that I did not know :) – quirimmo Jan 13 '19 at 17:29
  • @quirimmo do you know how to set NODE_ICU_DATA ? As`Node will use this ICU datafile if the environment variable NODE_ICU_DATA is set to “C:\Users\jeffo\AppData\Roaming\npm\node_modules\full-icu”` – jeffng50 Jan 13 '19 at 17:38
  • 1
    I think you should be able to that simply executing your node.js app through `NODE_ICU_DATA=“C:\Users\jeffo\AppData\Roaming\npm\node_modules\full-icu” node MY_INDEX.js`. But I did not really understand how this path can fix the issue, at least provide it dynamically otherwise you have a static path to your hard drive – quirimmo Jan 13 '19 at 17:42
  • @quirimmo yeah I fixed it by making the start script to execute my node.js app through that, thank you very much ! Now how do I make your comment as accepted answer? – jeffng50 Jan 13 '19 at 17:45
  • 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 Jan 13 '19 at 17:47
  • 1
    @quirimmo Done ! Can't thank you enough, been frustrated with this issue for hours. – jeffng50 Jan 13 '19 at 18:10
  • 1
    do not try to format the hours that you have been frustrated as datetime in node.js or you will have another issue! :) joking aside, happy to help and happy you solved it! – quirimmo Jan 13 '19 at 18:11
  • "en-GB" is an ISO 639-1 **language code**, it is not a locale. It is used by *toLocaleString* to infer user preferences. The parameter is called "locale" but is defined as a language code. E.g. de-ch, it-ch and fr-ch define 3 different languages that are all in the same small region or locality. – RobG Jan 13 '19 at 23:14

2 Answers2

5

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 !

jeffng50
  • 199
  • 3
  • 15
1

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"));
O.O
  • 1,389
  • 14
  • 29