When I use toLocaleDateString
in browser it returns
n = new Date()
n.toLocaleDateString()
\"2/10/2013\"
but in node.js the for
Date.prototype.toLocaleDateString = function () {
var d = new Date();
return (d.getMonth() + 1) + "/" + d.getDate() + "/" + d.getFullYear();
};
I also found this broken in node.JS. For example, in node console, type
new Date().toLocaleDateString('en-GB')
it still displays US format. Using Werner's method above, you can override default Date.toLocaleDateString() behavior for your locale:
Date.prototype.toLocaleDateString = function () {
return `${this.getDate()}/${this.getMonth() + 1}/${this.getFullYear()}`;
};
in node.JS you can not get browser's format. Node.JS runs on Server-side.
You have to do date formatting before displaying it in browser at your client side JS framework.
For me, the solution was to install an additional module full-icu
for node js
full-icu-npm
And after in package.json insert:
{"scripts":{"start":"node --icu-data-dir=node_modules/full-icu YOURAPP.js"}}
or
In the current version of Node.js v10.9.0 is Internationalization Support.
To control how ICU is used in Node.js, you can configure options are available during compilation.
If the small-icu
option is used you need to provide ICU data at runtime:
env NODE_ICU_DATA=/some/directory node
node --icu-data-dir=/some/directory