将12h时钟值转换为24h时钟值

时间:2015-07-03 02:01:43

标签: javascript angularjs time ecmascript-5

给定一些包含时钟值的字符串,例如“上午12.00 - 下午4.00”,“上午4.00 - 上午9.00”和“上午5.00 - 上午9:00,下午1:00-8:00”,如何将每个字符串转换为他们的24小时相当于“12:00-16:00”,“4.00-9.00”和“5.00-9.00,13.00-20.00”?

1 个答案:

答案 0 :(得分:0)

<form action="[posturl]" method="post" accept-charset="ISO-8859-1">
<table border="0" cellspacing="5" cellpadding="0" class="ssi_table">
<tr>
<td align="left" width="85"><label for="user"><b>Username:</b></label>&nbsp;</td>
<td><input type="text" id="user" name="user" size="9" value="" class="input_text"/></td>
</tr><tr>
<td align="left"><label for="passwrd"><b>Password:</b></label>&nbsp;</td>
<td><input type="password" name="passwrd" id="passwrd" size="9" class="input_password"/></td>
</tr><tr>
<td><input type="hidden" name="cookielength" value="-1"/></td>
</tr>
</table>
<center><input type="submit" value="Login" value="Login" class="button_submit"/></center>
</form>

我用javascript和regex试了一下。

var str = "12.00 am - 4.00 pm\n4.00 - 9.00 am\n5.00 am - 9.00 am\n1.00 - 8.00 pm";
var reg = /(\d+\.\d+)( am| pm|)? - (\d+\.\d+) (am|pm)/g;
str.replace(reg,function myfun(g,g1,g2,g3,g4){
    if(!g2){
        g2 = g4
    }
    if(g2=='pm'){
        g1 = (parseInt(g1)+12)+".00";
    }
    if(g4=='pm'){
        g3 = (parseInt(g3)+12)+".00";
    }
    return g1+" - "+g3;
});