node.js - 无法覆盖 node.js 中的 Date toString
问题描述
在 node.js 中:
Date.prototype.toString = function dateToString() {
return `${this.getMonth()}/${this.getDate()} of ${this.getFullYear()}`
};
console.log("====>", new Date(2019, 0, 1))
我预计“2019 年 2 月 11 日”,而不是“2019-01-01T02:00:00.000Z”。
node.js 坏了吗?
解决方案
我认为 node.js 没有坏。但是你需要调用 toString() 来获取 console.log 中的字符串
Date.prototype.toString = function dateToString() {
return `${this.getMonth()}/${this.getDate()} of ${this.getFullYear()}`
};
var date = new Date(2019, 0, 1);
console.log("====>", date.toString());
console.log("====>", date.toDateString());
输出:
====> 2019 年 0 月 1 日
====> 2019 年 1 月 1 日星期二