Add multiple DateTime Duration strings together using JavaScript to get total duration

前端 未结 4 1517
春和景丽
春和景丽 2021-01-16 07:36

I have an HTML Table used to generate a Calendar which shows TimeClock entries for each day a user has worked and clocked in and out of the system. Each day also shows the

4条回答
  •  伪装坚强ぢ
    2021-01-16 07:49

    With JQuery and Javascript its easily possible. Please have a look at below code.

    $(document).ready(function(){
        var pad = function(num) { return ("0"+num).slice(-2); }
        var totalSeconds = 0;
        $("li").each(function(){
            var currentDuration = $(this).text();
            currentDuration = currentDuration.split(":");
            var hrs = parseInt(currentDuration[0],10);
            var min = parseInt(currentDuration[1],10);
            var sec = parseInt(currentDuration[2],10);
            var currDurationSec = sec + (60*min) + (60*60*hrs); 
            totalSeconds +=currDurationSec;
        });
        
    
        var hours = Math.floor(totalSeconds / 3600);
    	totalSeconds %= 3600;
    	var minutes = Math.floor(totalSeconds / 60);
    	var seconds = totalSeconds % 60;
      $(".totalVal").text(pad(hours)+":"+pad(minutes)+":"+pad(seconds));
    });
    
    
    • 03:31:23
    • 04:21:56
    • 04:08:42
    • 03:31:17
    • 04:10:59
    • 02:48:21
    • 04:26:11
    • 00:00:39
    • 03:41:37
    Total Time:

提交回复
热议问题