I am having similar requirement as this: Convert time in HH:MM:SS format to seconds only?
but in javascript. I have seen many examples of converting seconds into different formats but not HH:MM:SS into seconds. Any help would be appreciated.
I am having similar requirement as this: Convert time in HH:MM:SS format to seconds only?
but in javascript. I have seen many examples of converting seconds into different formats but not HH:MM:SS into seconds. Any help would be appreciated.
try
This function works for MM:SS as well:
Try this:
This can be done quite resiliently with the following:
This is because each unit of time within the hours, minutes and seconds is a multiple of 60 greater than the smaller unit. Time is split into hour minutes and seconds components, then reduced to seconds by using the accumulated value of the higher units multiplied by 60 as it goes through each unit.
The
+time
is used to cast the time to a number.It basically ends up doing:
(60 * ((60 * HHHH) + MM)) + SS
Javascript's static method
Date.UTC()
does the trick: