如果使用的是Node.js,则一定要具有Ecmascript 5,因此Date具有
toISOString方法。您要对ISO8601进行一点修改:
new Date().toISOString()> '2012-11-04T14:51:06.157Z'
因此,只需删掉一些内容,您就可以设置好:
new Date().toISOString(). replace(/T/, ' '). // replace T with a space replace(/..+/, '') // delete the dot and everything after> '2012-11-04 14:55:45'
或者,一行:
new Date().toISOString().replace(/T/, ' ').replace(/..+/, '')
ISO8601必定是UTC(在第一个结果中也由尾随Z指示),因此默认情况下您会获得UTC(总是一件好事)。



