English 中文(简体)
1. 转换时间定位格式
原标题: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 ;
    }
最佳回答
问题回答

我想将几乎任何形式的时间格式改为HHH:mm:javacript

http://www.datejs.com/“rel=“nofollow”http://www.datejs.com/

没有理由重新发明轮.。

然而,如果要求你执行这一紧急情况,那么我认为马克解决办法将有助于

如果,请重新使用else>,这就要求前面的所有有条件的区块都等同于伪造。

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

或许,你应该使用被俘虏的群体,而不是再 par:

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




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.