简体   繁体   中英

convert time string format

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';
    }

I think you are using indexOf incorrectly here:

if(timeString.indexOf('.')){

From the documentation:

Returns the first index at which a given element can be found in the array, or -1 if it is not present.

Probably you mean this:

if(timeString.indexOf('.') > -1) {

With your code the expression in the first if statement will be true even if the string does not contain a dot. This means that the else if statement will never be executed.

I want to convert a almost any kind of time format to the format HH:mm:ss in javacript

Check this out: http://www.datejs.com/

There's no reason to re-invent the wheel.

However, if you are required to implement this yourself, then I believe Mark's solution will help

You're using else if , which requires that all preceding conditional blocks equate to false.

Try this:

    if(timeString.indexOf('.')){
        hoursEndIndex = timeString.indexOf('.');
        minutesStartIndex = timeString.indexOf('.') + 1;
    }
    if(timeString.length == 3){
        hoursEndIndex = 1;
        minutesStartIndex = 1;
    } else if(timeString.length == 4){
        hoursEndIndex = 2;
        minutesStartIndex = 2;
        return timeString.length;
    }

Perhaps you should use captured groups instead of parsing the string again:

var groups = regEx2.exec(timeString);
if(groups){
    var hours = groups[0];
    if(hours.length == 1){
        hours = '0' + hours;
    }
    var minutes = groups[1];
    return hours + ":" + minutes + ":00";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM