我想将日期转换为时间戳,我的输入是26-02-2012。我使用

new Date(myDate).getTime();

上面写着NaN..有人能告诉我怎么转换吗?


当前回答

function getTimeStamp() {
       var now = new Date();
       return ((now.getMonth() + 1) + '/' + (now.getDate()) + '/' + now.getFullYear() + " " + now.getHours() + ':'
                     + ((now.getMinutes() < 10) ? ("0" + now.getMinutes()) : (now.getMinutes())) + ':' + ((now.getSeconds() < 10) ? ("0" + now
                     .getSeconds()) : (now.getSeconds())));
}

其他回答

简单地在Date对象上执行一些算术运算将返回时间戳作为数字。这对于简洁表示法很有用。我发现这是最容易记住的方法,因为该方法也适用于将转换为字符串类型的数字转换回数字类型。

let d = new Date(); Console.log (d, d * 1);

+新的日期(替换) 这应该将myDate转换为timeStamp

对于那些希望具有yyyymmddHHMMSS格式的可读时间戳的人

> (new Date()).toISOString().replace(/[^\d]/g,'')              // "20190220044724404"
> (new Date()).toISOString().replace(/[^\d]/g,'').slice(0, -3) // "20190220044724"
> (new Date()).toISOString().replace(/[^\d]/g,'').slice(0, -9) // "20190220"

使用示例:备份文件扩展名。/我的/ / my.file.js.20190220路径

为了将(ISO)日期转换为Unix时间戳,我最终得到了一个比所需时间长3个字符的时间戳,所以我的年份大约是50k…

我要除以1000 new Date('2012-02-26').getTime() / 1000

只是提醒一下

Date.parse(“2022 - 08 - 04 - t04:02:10.909z”)

1659585730909

日期。解析(新日期(“2022 - 08 - 04 - t04:02:10.909z”))

1659585730000