一起加2次 [英] adding 2 times together
问题描述
格式hh:mm:ss任何建议都会很棒谢谢Todd
" Stormkid" < BA ***** @ goldengate.net>在消息中写道
news:41 ********************* @ newsreader.goldengate .net ...Hey Gang,我正在试图找出最好的方法一起添加两次
格式hh:mm:ss任何建议都会很棒谢谢Todd
将所有转换为秒,添加,然后转换回来。通过这种方式,您可以在第三次轻松添加
。例如:
var t1 =''12:34:56'';
var t2 =''10:10:03'';
t1 = t1.split(/ \ D /);
t2 = t2.split(/ \ D /);
var x1 = parseInt(t1 [0])* 60 * 60 + parseInt(t1 [1])* 60 + parseInt(t1 [2]);
var x2 = parseInt(t2 [0])* 60 * 60 + parseInt(t2 [1])* 60 + parseInt(t2 [2]);
var s = x1 + x2;
var m = Math.floor(s / 60); s = s%60;
var h = Math.floor(m / 60); m = m%60;
var d = Math.floor(h / 24); h = h%24;
alert(d +'':''+ h +'':''+ m +'':''+ s);
您可能希望将前导零'添加到10以下的数字等。
--Iv
Ivo写道:
[snip]
将所有转换为秒,添加,然后转换回来。通过这种方式,您可以在第三次轻松添加
。例如:
[snip]
这是对Ivo示例的修改,我创建了toSec(), toHr()
和toDay()函数。当然没有输入验证,
实际功能可能会更高效。
你可能仍然想要添加前导零并包括toHr ()或
toDay(),而不是两者。
使用更正确
这个.form.elements [''time1'']。值
而不是更短但可能同样可靠:
this.form .time1.value
干杯,弗雷德。
< script type =" text / javascript">
功能toSec(t){
var b = t.split(/ \ D /);
return(+ b [0])* 60 * 60 +(+ b [1])* 60 +(+ b [2]);
}
函数toHr(s){
var m = Math.floor(s / 60);
var h = Math.floor(m / 60);
返回h +'':''+ m%60 +'':''+ s%60;
}
函数toDay(s){
var m = Math.floor(s / 60);
var h = Math.floor(m / 60);
var d = Math.floor(h / 24);
返回d +'':''+ h%24 +'':''+ m%60 +'':''+ s%60 ;
}
< / script>
< p>输入格式时间:hh:mm:ss< / p>
< form name =" fred" action ="">
< input type =" text"名称= QUOT;时间1" cols =" 10"& nbsp;& nbsp;
< input type =" text"名称= QUOT;时间2" cols =" 10">< br>
< input type =" button" value ="添加时间" onclick ="
alert(''Hours:''+ toHr(toSec(this.form.time1.value)
+ toSec(this.form.time2。价值))
+''\ nDays:''+ toDay(toSec(this.form.time1.value)
+ toSec(this.form.time2。价值)));
">
< / form>
Fred Oz写道:
[snip]您可能仍想添加前导零...
[snip]
这里是'添加前导零'如果传递的数字只有一位数,则会添加
前导零的函数:
函数addZero(x){
if(String(x)。length< 2)x =''0''+ x;
返回x;
}
像这样在toHr()
返回addZero(h)+'':''+ addZero(m%60)+'':''
+ addZero(s%60);
Rob
Hey Gang, I''m trying to figure out the best way to add two times together of
the format hh:mm:ss any suggestions would be great thanks Todd
"Stormkid" <ba*****@goldengate.net> wrote in message
news:41*********************@newsreader.goldengate .net...Hey Gang, I''m trying to figure out the best way to add two times together of the format hh:mm:ss any suggestions would be great thanks Todd
Convert all to seconds, add, then convert back. This way you can easily add
a third time later. For example:
var t1=''12:34:56'';
var t2=''10:10:03'';
t1=t1.split(/\D/);
t2=t2.split(/\D/);
var x1=parseInt(t1[0])*60*60 + parseInt(t1[1])*60 + parseInt(t1[2]);
var x2=parseInt(t2[0])*60*60 + parseInt(t2[1])*60 + parseInt(t2[2]);
var s=x1+x2;
var m=Math.floor(s/60); s=s%60;
var h=Math.floor(m/60); m=m%60;
var d=Math.floor(h/24); h=h%24;
alert(d+'':''+h+'':''+m+'':''+s);
You may wish to add leading zero''s to numbers below 10 etc.
--Iv
Ivo wrote:
[snip]
Convert all to seconds, add, then convert back. This way you can easily add
a third time later. For example:
[snip]
Here is a modification of Ivo''s example, I''ve created toSec(), toHr()
and toDay() functions. Of course there is no input validation and the
actual functions can probably be made more efficient.
You may still want to add leading zeros and include either toHr() or
toDay(), not both.
It is also more correct to use
this.form.elements[''time1''].value
rather than the shorter but probably just as reliable:
this.form.time1.value
Cheers, Fred.
<script type="text/javascript">
function toSec(t) {
var b = t.split(/\D/);
return (+b[0])*60*60 + (+b[1])*60 + (+b[2]);
}
function toHr(s){
var m = Math.floor(s/60);
var h = Math.floor(m/60);
return h + '':'' + m%60 + '':'' + s%60;
}
function toDay(s){
var m = Math.floor(s/60);
var h = Math.floor(m/60);
var d = Math.floor(h/24);
return d + '':'' + h%24 + '':'' + m%60 + '':'' + s%60;
}
</script>
<p>Enter times of format: hh:mm:ss</p>
<form name="fred" action="">
<input type="text" name="time1" cols="10">
<input type="text" name="time2" cols="10"><br>
<input type="button" value="Add times" onclick="
alert(''Hours: '' + toHr(toSec(this.form.time1.value)
+ toSec(this.form.time2.value))
+ ''\nDays: '' + toDay(toSec(this.form.time1.value)
+ toSec(this.form.time2.value)));
">
</form>
Fred Oz wrote:
[snip]You may still want to add leading zeros ...
[snip]
Here''s an "add leading zeros" function that will add a
leading zero if the number passed has only one digit:
function addZero(x) {
if (String(x).length < 2) x = ''0'' + x;
return x;
}
Use it like this in toHr()
return addZero(h) + '':'' + addZero(m%60) + '':''
+ addZero(s%60);
Rob
这篇关于一起加2次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!