我正在尝试在Node.js中将日期字符串转换为Unix时间戳。
我的代码在客户端上运行得非常完美,但是当我在服务器上运行它时,出现了错误:
(node:19260) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: input.substring is not a function
我的代码如下:
function dateParser(input) {
// function is passed a date and parses it to create a unix timestamp
// removing the '.000' from input
let finalDate = input.substring(0, input.length - 4);
return new Date(finalDate.split(' ').join('T')).getTime();
}
我的输入示例是2017-09-15 00:00:00.000
那么为什么上述代码在客户端可以运行,但在 Node 中却不能正常工作?如何在 Node 中复制该功能?
dateParser()
被如何调用并传递了什么参数? - Brett DeWoodyconsole.log(typeof input)
吗? - TGrifDate.parse()
来获取日期的Unix时间戳表示。 - TGrif