Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery convertion from string to time

I have a system in development which records various times of the day in the following format: 06:53:22 or 19:23:58 as examples.

Can anyone tell me if it is possible to convert this string into javascript construct which I can use to compare times of the day?

like image 421
sisko Avatar asked Dec 01 '22 01:12

sisko


1 Answers

You can parse the time with this:

function timeToSeconds(time) {
    time = time.split(/:/);
    return time[0] * 3600 + time[1] * 60 + time[2];
}

It returns a number of seconds since 00:00:00, so you can compare the results easily:

timeToSeconds('06:53:22') < timeToSeconds('19:23:58')
like image 173
Arnaud Le Blanc Avatar answered Dec 05 '22 05:12

Arnaud Le Blanc