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

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

    See JsFiddle DEMO

        var date1 = new Date();    
        var date2 = new Date("2025/07/30 21:59:00");
        //Customise date2 for your required future time
    
        showDiff();
    
    function showDiff(date1, date2){
    
        var diff = (date2 - date1)/1000;
        diff = Math.abs(Math.floor(diff));
    
        var days = Math.floor(diff/(24*60*60));
        var leftSec = diff - days * 24*60*60;
    
        var hrs = Math.floor(leftSec/(60*60));
        var leftSec = leftSec - hrs * 60*60;
    
        var min = Math.floor(leftSec/(60));
        var leftSec = leftSec - min * 60;
    
        document.getElementById("showTime").innerHTML = "You have " + days + " days " + hrs + " hours " + min + " minutes and " + leftSec + " seconds before death.";
    
    setTimeout(showDiff,1000);
    }
    

    for your HTML Code:

    <div id="showTime"></div>
    
    0 讨论(0)
  • 2020-11-22 03:46

    If you use Date objects and then use the getTime() function for both dates it will give you their respective times since Jan 1, 1970 in a number value. You can then get the difference between these numbers.

    If that doesn't help you out, check out the complete documentation: http://www.w3schools.com/jsref/jsref_obj_date.asp

    0 讨论(0)
  • JavaScript perfectly supports date difference out of the box

    https://jsfiddle.net/b9chris/v5twbe3h/

    var msMinute = 60*1000, 
        msDay = 60*60*24*1000,
        a = new Date(2012, 2, 12, 23, 59, 59),
        b = new Date("2013 march 12");
    
    
    console.log(Math.floor((b - a) / msDay) + ' full days between'); // 364
    console.log(Math.floor(((b - a) % msDay) / msMinute) + ' full minutes between'); // 0
    

    Now some pitfalls. Try this:

    console.log(a - 10); // 1331614798990
    console.log(a + 10); // mixed string
    

    So if you have risk of adding a number and Date, convert Date to number directly.

    console.log(a.getTime() - 10); // 1331614798990
    console.log(a.getTime() + 10); // 1331614799010
    

    My fist example demonstrates the power of Date object but it actually appears to be a time bomb

    0 讨论(0)
  • 2020-11-22 03:50

    Depending on your needs, this function will calculate the difference between the 2 days, and return a result in days decimal.

    // This one returns a signed decimal. The sign indicates past or future.
    
    this.getDateDiff = function(date1, date2) {
        return (date1.getTime() - date2.getTime()) / (1000 * 60 * 60 * 24);
    }
    
    // This one always returns a positive decimal. (Suggested by Koen below)
    
    this.getDateDiff = function(date1, date2) {
        return Math.abs((date1.getTime() - date2.getTime()) / (1000 * 60 * 60 * 24));
    }
    
    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
  • 2020-11-22 03:50

    alternative modificitaion extended code..

    http://jsfiddle.net/vvGPQ/48/

    showDiff();
    
    function showDiff(){
    var date1 = new Date("2013/01/18 06:59:00");   
    var date2 = new Date();
    //Customise date2 for your required future time
    
    var diff = (date2 - date1)/1000;
    var diff = Math.abs(Math.floor(diff));
    
    var years = Math.floor(diff/(365*24*60*60));
    var leftSec = diff - years * 365*24*60*60;
    
    var month = Math.floor(leftSec/((365/12)*24*60*60));
    var leftSec = leftSec - month * (365/12)*24*60*60;    
    
    var days = Math.floor(leftSec/(24*60*60));
    var leftSec = leftSec - days * 24*60*60;
    
    var hrs = Math.floor(leftSec/(60*60));
    var leftSec = leftSec - hrs * 60*60;
    
    var min = Math.floor(leftSec/(60));
    var leftSec = leftSec - min * 60;
    
    
    
    
    document.getElementById("showTime").innerHTML = "You have " + years + " years "+ month + " month " + days + " days " + hrs + " hours " + min + " minutes and " + leftSec + " seconds the life time has passed.";
    
    setTimeout(showDiff,1000);
    }
    
    0 讨论(0)
提交回复
热议问题