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

前端 未结 16 2662
北海茫月
北海茫月 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:50

    function compare()
    {
      var end_actual_time    = $('#date3').val();
    
      start_actual_time = new Date();
      end_actual_time = new Date(end_actual_time);
    
      var diff = end_actual_time-start_actual_time;
    
      var diffSeconds = diff/1000;
      var HH = Math.floor(diffSeconds/3600);
      var MM = Math.floor(diffSeconds%3600)/60;
    
      var formatted = ((HH < 10)?("0" + HH):HH) + ":" + ((MM < 10)?("0" + MM):MM)
      getTime(diffSeconds);
    }
    function getTime(seconds) {
      var days = Math.floor(leftover / 86400);
    
      //how many seconds are left
      leftover = leftover - (days * 86400);
    
      //how many full hours fits in the amount of leftover seconds
      var hours = Math.floor(leftover / 3600);
    
      //how many seconds are left
      leftover = leftover - (hours * 3600);
    
      //how many minutes fits in the amount of leftover seconds
      var minutes = leftover / 60;
    
      //how many seconds are left
      //leftover = leftover - (minutes * 60);
      alert(days + ':' + hours + ':' + minutes);
    }
    

提交回复
热议问题