我想将日期转换为时间戳,我的输入是26-02-2012。我使用
new Date(myDate).getTime();
上面写着NaN..有人能告诉我怎么转换吗?
我想将日期转换为时间戳,我的输入是26-02-2012。我使用
new Date(myDate).getTime();
上面写着NaN..有人能告诉我怎么转换吗?
当前回答
简单地在Date对象上执行一些算术运算将返回时间戳作为数字。这对于简洁表示法很有用。我发现这是最容易记住的方法,因为该方法也适用于将转换为字符串类型的数字转换回数字类型。
let d = new Date(); Console.log (d, d * 1);
其他回答
只是提醒一下
Date.parse(“2022 - 08 - 04 - t04:02:10.909z”)
1659585730909
日期。解析(新日期(“2022 - 08 - 04 - t04:02:10.909z”))
1659585730000
+新的日期(替换) 这应该将myDate转换为timeStamp
将字符串拆分为多个部分,并将它们直接提供给Date构造函数:
更新:
这是计划 捐献 var新约会=新日期(单身约会,2—1—1,单身约会[0]); console.log (newDate.getTime ());
这个重构的代码就可以做到这一点
let toTimestamp = strDate => Date.parse(strDate)
这适用于除ie8-以外的所有现代浏览器
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())));
}