How to get current date without time?

﹥>﹥吖頭↗ 提交于 2020-05-12 11:18:40

问题


I'm trying to get the current date without the time and store it in a variable, within JavaScript. It needs to be without time as I'm converting it to an epoch date, with which I will use to measure the past 24 hours (if date is within 24 hours then it will be displayed). The problem is that with the added time, it doesn't match as within the last 24 hours.

e.g. it returns the date as the following when converted to epoch: 1408704590485

I want it to be like 1408662000000

I'm not to sure how to do this.

Code - How the current days epoch date is currently being stored -

var epochLoggingFrom;
var epochLoggingTo;

$(document).ready(function () {
    epochLoggingFrom = dateToEpoch(new Date());
    epochLoggingTo = dateToEpoch(new Date());
}

dateToEpoch function -

function dateToEpoch(thedate) {
    return thedate.getTime();
}

回答1:


Try this:

function dateToEpoch(thedate) {
    var time = thedate.getTime();
    return time - (time % 86400000);
}

or this:

function dateToEpoch2(thedate) {
   return thedate.setHours(0,0,0,0);
}

Example : http://jsfiddle.net/chns490n/1/

Reference: (Number) Date.prototype.setHours(hour, min, sec, millisec)




回答2:


Try this:

var nowDate = new Date(); 
var date = nowDate.getFullYear()+'/'+(nowDate.getMonth()+1)+'/'+nowDate.getDate(); 

Note: Adjust format as you want, like reorder day, month, year, remove '/' and get combined date etc.



来源:https://stackoverflow.com/questions/25445377/how-to-get-current-date-without-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!