I made the following function to get the number of seconds from a time string.
function TimeStringToSeconds(TimeString){
var TotalTime = 0;
var SplitString = TimeString.toString().split(':');
TotalTime += parseInt(SplitString[0])*3600;
TotalTime += parseInt(SplitString[1])*60;
TotalTime += parseInt(SplitString[2]);
return TotalTime;
}
It works, but not if the input contains an 8 or an 9, example:
TimeStringToSeconds('00:01:00'); // Outputs 60
TimeStringToSeconds('00:02:00'); // Outputs 120
TimeStringToSeconds('00:08:00'); // Outputs 0 ???????
What am i doing wrong?
Always include the radix:
Without it,
"08"is parsed in octal base, with8being an invalid digit.See also: parseInt at MDN