node.js Date.now().toISOString() 抛出错误“不是函数”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39217275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:32:02  来源:igfitidea点击:

Date.now().toISOString() throwing error "not a function"

node.js

提问by rmcneilly

I am running Node v6.4.0 on Windows 10. In one of my Javascript files I am trying to get an ISO date string from the Date object:

我在 Windows 10 上运行 Node v6.4.0。在我的一个 Javascript 文件中,我试图从 Date 对象获取 ISO 日期字符串:

let timestamp = Date.now().toISOString();

This throws: Date.now(...).toISOString is not a function

这抛出:Date.now(...).toISOString 不是函数

Looking through stackoverflow this should work...possible bug in Node?

查看 stackoverflow 这应该可以工作……Node 中可能存在错误?

回答by Adrian T

Date.now()returns a number which represents the number of milliseconds elapsed since the UNIX epoch. The toISOStringmethod cannot be called on a number, but only on a Dateobject, like this:

Date.now()返回一个数字,表示自 UNIX 纪元以来经过的毫秒数。该toISOString方法不能在数字上调用,而只能在Date对象上调用,如下所示:

var now = new Date();
var isoString = now.toISOString();

Or in one single line:

或者在一行中:

new Date().toISOString()