Date toLocaleDateString in node

前端 未结 4 1787
慢半拍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: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()}`;
    };
    

提交回复
热议问题