NodeJS的日期类型缺省显示的格式是怎么来的
发布于 7 年前 作者 chenxiaohu 3143 次浏览 来自 问答

执行 console.log(new Date)会显示类似:2017-09-12T02:22:34.806Z 这样格式的字符串,我想替换成 2017-09-12 02:22:34,但是不知道调用啥方法得到前面的格式,有谁知道不?谢谢:)

6 回复

去了解 moment

找到了。直接用这个,不用转了。。new Date().toLocaleString()

Date.prototype.toString=function(){
  return `${this.getFullYear()}-${(this.getMonth()+1+'').padStart(2,'0')}-${(''+this.getDate()).padStart(2,'0')} ${(''+this.getHours()).padStart(2,'0')}:${(''+this.getMinutes()).padStart(2,'0')}:${(''+this.getSeconds()).padStart(2,'0')}`;
}

在chrome里可以直接输出,node里还是要toString或者+’’

new Date() 一下就写个时间格式化方法,不用moment吧

不用引入moment库吧

不管是 nodejs 还是浏览器,都对一些特殊对象进行了特殊处理,当然最显著的就是 Dete,输出时看起来是 字符串,其实是 Date 对象

回到顶部