Date toLocaleDateString in node

前端 未结 4 1777
慢半拍i
慢半拍i 2021-01-04 04:49

When I use toLocaleDateString in browser it returns

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

but in node.js the for

相关标签:
4条回答
  • 2021-01-04 05:25
    Date.prototype.toLocaleDateString = function () {
      var d = new Date();
      return (d.getMonth() + 1) + "/" + d.getDate() + "/" + d.getFullYear();
    };
    
    0 讨论(0)
  • 2021-01-04 05:27

    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()}`;
    };
    
    0 讨论(0)
  • 2021-01-04 05:28

    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.

    0 讨论(0)
  • 2021-01-04 05:47

    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:

    • the NODE_ICU_DATA environment variable: 
env NODE_ICU_DATA=/some/directory node
    • the --icu-data-dir CLI parameter:
 node --icu-data-dir=/some/directory

    0 讨论(0)
提交回复
热议问题