How do I get the difference between two Dates in JavaScript?

前端 未结 16 2391
北海茫月
北海茫月 2020-11-22 03:03

I\'m creating an application which lets you define events with a time frame. I want to automatically fill in the end date when the user selects or changes the start date.

16条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 04:00

    var getDaysLeft = function (date1, date2) {
       var daysDiffInMilliSec = Math.abs(new Date(date1) - new Date(date2));
       var daysLeft = daysDiffInMilliSec / (1000 * 60 * 60 * 24);   
       return daysLeft;
    };
    var date1='2018-05-18';
    var date2='2018-05-25';
    var dateDiff = getDaysLeft(date1, date2);
    console.log(dateDiff);
    

提交回复
热议问题