Date toLocaleDateString in node

When I use toLocaleDateStringin the browser, it returns

n = new Date()
n.toLocaleDateString()
"2/10/2013"

but in node.js format the format is completely different

n = new Date()
> n.toLocaleDateString()
'Sunday, February 10, 2013'

How to get browser format ( mm/dd/yy) in node.js?

+5
source share
4 answers
Date.prototype.toLocaleDateString = function () {
  var d = new Date();
  return (d.getMonth() + 1) + "/" + d.getDate() + "/" + d.getFullYear();
};
+2
source

I also found this broken in node.JS. For example, in the node console, type

new Date().toLocaleDateString('en-GB')

It still displays US format. Using the Werner method above, you can override the default behavior of Date.toLocaleDateString () for your language:

Date.prototype.toLocaleDateString = function () {
    return `${this.getDate()}/${this.getMonth() + 1}/${this.getFullYear()}`;
};
+3
source

full-icu node js full-icu-npm

package.json :

{"scripts":{"start":"node --icu-data-dir=node_modules/full-icu YOURAPP.js"}}
0

node.JS . Node.JS .

, JS .

-3

All Articles