Check time difference in Javascript

后端 未结 18 1171
梦毁少年i
梦毁少年i 2020-11-22 04:27

How would you check time difference from two text-boxes in Javascript?

18条回答
  •  梦毁少年i
    2020-11-22 05:00

    A good solution is avaliable at

    http://blogs.digitss.com/javascript/calculate-datetime-difference-simple-javascript-code-snippet/

    gives the output in your desired differnece format of

    days : hours : minutes : seconds .

    A slightly modified version of that code is shown below

     var vdaysdiff; // difference of the dates
       var vhourDiff;
       var vmindiff;
       var vsecdiff;
    
       vdaysdiff = Math.floor(diff/1000/60/60/24);  // in days
       diff -= vdaysdiff*1000*60*60*24;
    
       vhourDiff = Math.floor(diff/1000/60/60);  // in hours
       diff -= vhourDiff*1000*60*60;
    
       vmindiff = Math.floor(diff/1000/60); // in minutes
       diff -= vmindiff*1000*60;
    
       vsecdiff= Math.floor(diff/1000);  // in seconds
    
       //Text formatting
       var hourtext = '00';
       if (hourDiff > 0){ hourtext = String(hourDiff);}
       if (hourtext.length == 1){hourtext = '0' + hourtext};                                                              
    
       var mintext = '00';                           
       if (mindiff > 0){ mintext = String(mindiff);}
       if (mintext.length == 1){mintext = '0' + mintext};
    
      //shows output as HH:MM ( i needed shorter duration)
       duration.value= hourtext + ':' + mintext;
    

提交回复
热议问题