I want to convert time data to the format HH:mm:ss
in JavaScript.
I ve got a problem in my code (see comments inside the code):
function parseTime(timeString){
var timeString = timeString.toLowerCase();
timeString = $.trim(timeString);
var regEx = /^([0-9]|1[0-9]|2[0-3])$/;
var regEx2 = /^([0-9]|1[0-9]|2[0-3]).?([0-5][0-9])$/;
var regEx3 = /^([0-9]|1[0-2])(a|p|am|pm)$/;
var regEx4 = /^([1-9]|10|11|12).?([0-5][0-9])(a|p|am|pm)$/;
if(regEx.test(timeString)){
var hours = timeString;
if(hours.length == 1){
hours = 0 + hours;
}
return hours + :00:00 ;
}
else if(regEx2.test(timeString)){
var hoursEndIndex, minutesStartIndex;
if(timeString.indexOf( . )){
hoursEndIndex = timeString.indexOf( . );
minutesStartIndex = timeString.indexOf( . ) + 1;
}else if(timeString.length == 3){//Problem here timeString.length returns 3 but the code below isn t executed?
hoursEndIndex = 1;
minutesStartIndex = 1;
}else if(timeString.length == 4){//Same thing here?
hoursEndIndex = 2;
minutesStartIndex = 2;
return timeString.length;
}
var hours = timeString.substring(0, hoursEndIndex);
if(hours.length == 1){
hours = 0 + hours;
}
var minutes = timeString.substr(minutesStartIndex, 2);
return hours + : + minutes + :00 ;
}