- #1
Jamin2112
- 986
- 12
I made this to convert a time in hh:mm:ss form to seconds. Not working, for some reason. I tested "00:02:29" and got "2". Any idea why? Also, any ideas on how I can make this procedure more simple and elegant?
Code:
function clock2secs(clockTime)
{
if (clockTime.length > 8)
{
return -1;
}
if (isDecimal(clockTime.charAt(0)) && isDecimal(clockTime.charAt(1)))
{
var hrs = parseInt(clockTime.substring(0,1));
} else
{
return -1;
}
if (clockTime.charAt(2) != ':')
{
return -1;
}
if (isDecimal(clockTime.charAt(3)) && isDecimal(clockTime.charAt(4)))
{
var mins = parseInt(clockTime.substring(3,4));
} else
{
return -1;
}
if (clockTime.charAt(5) != ':')
{
return -1;
}
if (isDecimal(clockTime.charAt(6)) && isDecimal(clockTime.charAt(7)))
{
var secs = parseInt(clockTime.substring(6,7));
} else
{
return -1;
}
/* If here, clockTime was in valid format hh:mm:ss, and its
minutes and seconds were parsed correctly.
*/
return 3600 * hrs + 60 * mins + secs;
}
function isDecimal(c)
{
return c <= '9' && c >= '0';
}
Last edited by a moderator: