Javascript compare two dates to get a difference

后端 未结 6 2098
长情又很酷
长情又很酷 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:11

    Try like this

    var date = new Date(input).getTime(); // Get the milliseconds
    var today = new Date();    
    
    //You can't compare date, 
    //so convert them to milliseconds
    today = new Date(today.setDate(today.getDate() + 7)).getTime(); 
    if (inputDate < today) {
        // The selected time is less than 7 days from now
        return false;
     } else if{ ()
        // -Exact- same timestamps.
        return false;
     }
     else {
        // The selected time is more than 7 days from now
        return true;
     }
    

提交回复
热议问题