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

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

    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);
    }
    

提交回复
热议问题