Advertisement
Virajsinh

Time Calculation in Java Script

Mar 5th, 2024 (edited)
978
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 0.92 KB | Source Code | 0 0
  1. // a_value = "04:22:00"; hh:mm:ss
  2. // b_value = "06:13:10"; hh:mm:ss
  3.  
  4. function time_calculation(a_value, b_value)
  5. {
  6.   var hour = 0;
  7.   var minute = 0;
  8.   var second = 0;
  9.  
  10.   var splitTime1 = a_value.toString().split(':');
  11.   var splitTime2 = b_value.toString().split(':');
  12.  
  13.   hour = parseInt(check_Int(splitTime1[0]))+parseInt(check_Int(splitTime2[0]));
  14.   minute = parseInt(check_Int(splitTime1[1]))+parseInt(check_Int(splitTime2[1]));
  15.   hour = hour + minute/60;
  16.   hour = Math.floor(hour); // Extra Add
  17.   minute = minute%60;
  18.   second = parseInt(check_Int(splitTime1[2]))+parseInt(check_Int(splitTime2[2]));
  19.   minute = minute + second/60;
  20.   second = second%60;
  21.   const zeroPad = (num, places) => String(num).padStart(places, '0');
  22.  
  23.   return zeroPad(hour, 2)+':'+zeroPad(minute, 2)+':'+zeroPad(second, 2);
  24. }
  25.  
  26. function check_Int(argument)
  27. {
  28.   if(argument % 1 === 0){
  29.     return argument;
  30.   }else{
  31.     return 0;
  32.   }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement