Skip to content
Advertisement

Insert SubString in a time input type

I have an time calculator which returns an input type of time in the following format: hh/mm/ss

It currently displays like this:

JavaScript

When I do a calculate function it return “3:0:0” instead and removes the “0”.

How can I change this to be 03:00:00? The “0” must only be added if the h/m/s is less then 10.

Here are my calculate function if needed. The function returns the sum of time fron an array:

JavaScript

Advertisement

Answer

You need to add the zeros after the reduce, since reduce is still calculating the time.

I added a .map after the calculation to get each value of time and then check if value is less than 10:

.map((v) => (v < 10) ? v = "0" + v : v)

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement