-2

I'm trying to add two times in this format:

HH:MM:SS

eg: 12:31:32 and 01:32:39

I want to write a script that adds times and the result of which must be in the same format.

How would you tell the script to add up the first two digits before : (hours) and then the two between the two semicolons (minutes) and then seconds?

I don't want anyone to make the script I just want to know how it could be made.

Brendan Green
  • 11,676
  • 5
  • 44
  • 76
New
  • 13
  • 1

1 Answers1

1

If they are strings var time1 = "1:23:25" then you will need to convert it into something else to work with it.

You can do it manually, by splitting the string into an array and adding the numbers and handling any overflow (>60 seconds to increase minutes etc) and then joining the hours, minutes and seconds back together to create a new string.

You can also convert each to a Date which you can just add together like they were numbers eg:time3=time1+time2 you can then format the result back into the original format.

NickSlash
  • 4,758
  • 3
  • 21
  • 38