JavaScript time convert
I am very new to JavaScript. My guess is that there is a very easy answe开发者_运维问答r.
I need to convert time in the format hh:mm:ss
to Seconds.
You can do something like this:
var s = "1:01:01";
var a = s.split(":");
var sec = parseInt(a[0], 10)*60*60;
sec += parseInt(a[1], 10)*60;
sec += parseInt(a[2], 10);
alert(sec);
try this
<SCRIPT LANGUAGE="JavaScript1.1"><!--
function split(string,text) {
splitArray = string.split(text);
splitIndex = splitArray.length;
}
//--></SCRIPT>
<SCRIPT LANGUAGE="JavaScript"><!--
var splitIndex = 0;
var splitArray = new Array();
function calculateTime() {
splitIndex = 0;
split(document.myForm.myTime.value,':');
for (var i=splitIndex-1, j=1, answer=0; i>=0; i=i-1, j=j*60)
answer += splitArray[i]*j - 0;
document.myForm.mySeconds.value = answer;
}
//--></SCRIPT>
<FORM NAME="myForm">
<P>HH:MM:SS = <INPUT NAME="myTime" TYPE="TEXT" MAXLENGTH="8" LENGTH="10">
<P>Seconds = <INPUT NAME="mySeconds" TYPE="TEXT">
<P><INPUT TYPE="BUTTON" VALUE="Calculate" onClick="calculateTime()">
</FORM>
精彩评论