Javascript compare two dates to get a difference

后端 未结 6 2099
长情又很酷
长情又很酷 2020-12-12 04:24

I am trying to compare two different dates to see if the date inputted is after 7 days of todays date. I have done a bit of googling and come up with this:

f         


        
6条回答
  •  清歌不尽
    2020-12-12 05:12

    Assuming that input is a valid Javascript Date object, you could perhaps try:

    function dateDifference(oldDate) {
        var currentDate = new Date();
        var difference = currentDate - oldDate; //unit: milliseconds
        var numDays = 7;
        var threshHoldTime = numDays * (24 * 60 * 60 * 1000); //seven days in milliseconds
        if (difference > threshHoldTime ) {
            console.log("The difference is greateer then then 7 days");
        }
        else {
            console.log("the date is not enough: " + difference);
        }
    }
    

提交回复
热议问题