How to get current date without time?

前端 未结 2 727
轻奢々
轻奢々 2021-01-07 23:05

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

相关标签:
2条回答
  • 2021-01-07 23:39

    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)

    0 讨论(0)
  • 2021-01-07 23:39

    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.

    0 讨论(0)
提交回复
热议问题