我想将日期转换为时间戳,我的输入是26-02-2012。我使用
new Date(myDate).getTime();
上面写着NaN..有人能告诉我怎么转换吗?
我想将日期转换为时间戳,我的输入是26-02-2012。我使用
new Date(myDate).getTime();
上面写着NaN..有人能告诉我怎么转换吗?
当前回答
其他开发人员已经提供了答案,但在我自己的方式中,你可以在不创建任何用户定义函数的情况下执行此操作,如下所示:
var timestamp = Date.parse("26-02-2012".split('-').reverse().join('-'));
alert(timestamp); // returns 1330214400000
其他回答
试试这个函数,它使用Date.parse()方法,不需要任何自定义逻辑:
function toTimestamp(strDate){
var datum = Date.parse(strDate);
return datum/1000;
}
alert(toTimestamp('02/13/2009 23:31:30'));
其他开发人员已经提供了答案,但在我自己的方式中,你可以在不创建任何用户定义函数的情况下执行此操作,如下所示:
var timestamp = Date.parse("26-02-2012".split('-').reverse().join('-'));
alert(timestamp); // returns 1330214400000
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构造函数:
更新:
这是计划 捐献 var新约会=新日期(单身约会,2—1—1,单身约会[0]); console.log (newDate.getTime ());
你只需要倒转你的日期数字,然后用,
new Date(2012,01,26).getTime(); // 02 becomes 01 because getMonth() method returns the month (from 0 to 11)
在你的情况下:
var myDate="26-02-2012";
myDate=myDate.split("-");
new Date(parseInt(myDate[2], 10), parseInt(myDate[1], 10) - 1 , parseInt(myDate[0]), 10).getTime();
附注:英国地区在这里并不重要。