我试图在没有时间的情况下获取当前日期并将其存储在JavaScript中的变量中。它需要没有时间,因为我将它转换为一个纪元日期,我将使用它来测量过去24小时(如果日期在24小时内,那么它将显示......)。问题是,随着时间的增加,它与过去24小时内不匹配。
e.g。转换为纪元时,它返回以下日期:1408704590485
我希望它像1408662000000
我不确定该怎么做。
代码 - 当前存储当前日期的时间 -
var epochLoggingFrom;
var epochLoggingTo;
$(document).ready(function () {
epochLoggingFrom = dateToEpoch(new Date());
epochLoggingTo = dateToEpoch(new Date());
}
dateToEpoch函数 -
function dateToEpoch(thedate) {
return thedate.getTime();
}
非常感谢帮助,谢谢!
答案 0 :(得分:13)
试试这个:
function dateToEpoch(thedate) {
var time = thedate.getTime();
return time - (time % 86400000);
}
或者这个:
function dateToEpoch2(thedate) {
return thedate.setHours(0,0,0,0);
}
示例:http://jsfiddle.net/chns490n/1/
参考:(Number) Date.prototype.setHours(hour, min, sec, millisec)
答案 1 :(得分:6)
试试这个:
var nowDate = new Date();
var date = nowDate.getFullYear()+'/'+(nowDate.getMonth()+1)+'/'+nowDate.getDate();
注意:根据需要调整格式,例如重新订购日,月,年,删除'/'并获取合并日期等。