index.tsx788 chars19 lines
We can define a function named formatTime
that takes the total time in seconds as a parameter. The function calculates the days, hours, minutes, and seconds from the given total seconds. Finally, it returns the formatted time as a string.
We are using the Math.floor()
function to convert the decimal values into integers. We are also using the modulo operator to calculate the remaining seconds, after the days and hours have been calculated.
Lastly, the formatted time string is built using conditional statements that check whether the days, hours, minutes and seconds are greater than 1 or not. This allows us to pluralize the units of time correctly in the output string.
gistlibby LogSnag