Skip to content Skip to sidebar Skip to footer

Adding HH:MM:SS Strings In Javascript

I don't think I can figure this out on my own. Lets say I have a time in the string format of HH:MM:SS ex. 10:11:06 and I would like to add another time to it and return it as a s

Solution 1:

Try this

var start = "10:11:06";
var end = "10:11:06";
  var a = start.split(":");
 var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]); 
 var b = end.split(":");
 var seconds2 = (+b[0]) * 60 * 60 + (+b[1]) * 60 + (+b[2]); 

 var date = new Date(1970,0,1);
     date.setSeconds(seconds + seconds2);

 var c = date.toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, "$1");
console.log(c);

Solution 2:

try

var c = date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
return c;

to convert date object to time.


Post a Comment for "Adding HH:MM:SS Strings In Javascript"