Dav*_*vis 2 javascript unix-timestamp node.js
我正在尝试将Date字符串转换为Node.js中的unix时间戳.
我的代码在我的客户端完美运行,但当我在我的服务器上运行时,我收到一个错误:
(node:19260)UnhandledPromiseRejectionWarning:未处理的promise拒绝(拒绝id:1):TypeError:input.substring不是函数
我的代码:
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();
}
Run Code Online (Sandbox Code Playgroud)
我输入的示例将是2017-09-15 00:00:00.000
那么为什么上面的工作在我的客户端而不是在Node中工作,我将如何复制节点中的功能?
Has*_*mam 11
从输入的dateTime字符串创建日期对象,然后使用getTime()并将结果除以1000以获取UNIX时间戳.
var unixTimestamp = Math.round(new Date("2017-09-15 00:00:00.000").getTime()/1000);
console.log(unixTimestamp);Run Code Online (Sandbox Code Playgroud)