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

前端 未结 16 2360
北海茫月
北海茫月 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条回答
  •  伪装坚强ぢ
    2020-11-22 03:59

    Below code will return the days left from today to futures date.

    Dependencies: jQuery and MomentJs.

    var getDaysLeft = function (date) {
      var today = new Date();
      var daysLeftInMilliSec = Math.abs(new Date(moment(today).format('YYYY-MM-DD')) - new Date(date));
      var daysLeft = daysLeftInMilliSec / (1000 * 60 * 60 * 24);   
      return daysLeft;
    };
    
    getDaysLeft('YYYY-MM-DD');
    

提交回复
热议问题