CNode

NodeJS的日期类型缺省显示的格式是怎么来的

问答
Cchenxiaohu发布于9 年前最后回复9 年前6 回复4193 浏览0 收藏

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

查看回复

回复 (6)

T
txg5214#1·9 年前

去了解 moment

C
chenxiaohu#2·9 年前

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

D
dislido#3·9 年前
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或者+''

F
FateZeros#4·9 年前

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

F
FateZeros#5·9 年前

不用引入moment库吧

H
Hyurl#6·9 年前

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

参与回复
登录后即可参与回复。登录