Time to Seconds and Seconds to Time in Javascript



下面有我的代码,

Before=document.getElementsByName("beforehr[]");
After=document.getElementsByName("afterhr[]");
MonthTotal=0
for(i=0;i<Before.length;i++){
BeforeInSeconds= // Convert Before[i].value to Seconds
AfterInSeconds= // Convert After[i].value to Seconds
MonthTotal=parseInt(MonthTotal)+ parseInt(BeforeInSeconds)+parseInt(AfterInSeconds);
}
MonthTotalHRS= // Convert MonthTotal value to Time
document.getElementById("txtMonthTotal").value=MonthTotal;
document.getElementById("Mthtotal").innerHTML=MonthTotalHRS;

我需要将Before Hours转换为Seconds,After Hours转换成Seconds,求和All the Seconds并转换为Time,然后将其放入Mthtotal

假设变量Before和After是数组。

var Before = [1, 2]; //180 Secs
var After = [3, 4]; // 420 Secs
var MonthTotal=0;
function secondsToHms(d) {  // Function to convert Secs to H:m:s
d = Number(d);
var h = Math.floor(d / 3600);
var m = Math.floor(d % 3600 / 60);
var s = Math.floor(d % 3600 % 60);
var hDisplay = h > 0 ? h + (h == 1 ? " hour " : " hours ") : "";
var mDisplay = m > 0 ? m + (m == 1 ? " minute " : " minutes ") : "";
var sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : "";
return hDisplay + mDisplay + sDisplay; 
}
for(i=0;i<Before.length;i++)
{
	BeforeInSeconds= Before[i] * 60;
	AfterInSeconds= After[i] * 60;
	MonthTotal=parseInt(MonthTotal)+ parseInt(BeforeInSeconds)+parseInt(AfterInSeconds);
}
console.log(MonthTotal); //600 Secs
var convertedop=secondsToHms(MonthTotal);
alert(convertedop);

您可以使用.split(':')将时间格式拆分为一个数组。其中,索引0是小时,索引1是分钟,索引2是秒。然后,您可以将每个时间单位转换为秒。

小时到秒:hour*3600

分钟到秒:minutes*60

秒到秒:seconds*1所以只有seconds

做所有这些都会给你你的总结果:

var before = [...document.getElementsByName("beforehr[]")];
var after = [...document.getElementsByName("afterhr[]")];
var monthTotal = 0
for (i = 0; i < before.length; i++) {
var beforeTime = before[i].value.split(':');
var afterTime = after[i].value.split(':');

var hourSeconds = +beforeTime[0] * 3600; // Convert the hours to seconds
var minuteSeconds = +beforeTime[1] * 60; // Convert the mins to secs
var seconds = +beforeTime[2]; // No conversions needed for secs to secs

var beforeInSeconds = hourSeconds + minuteSeconds + seconds;

// The above can be compresed into one line. I'll repeat the above for the afterTime on one line as an example:
var afterInSeconds = (+afterTime[0] * 3600) + (+afterTime[1] * 60) + (+afterTime[2])
monthTotal += parseInt(beforeInSeconds) + parseInt(afterInSeconds);
}
console.log("Month total in seconds", monthTotal)
// Hours, minutes and seconds (round down)
var hrs = ~~(monthTotal / 3600);
var mins = ~~((monthTotal % 3600) / 60);
var secs = ~~monthTotal % 60;
console.log("Month total in H:M:S", hrs +':' +mins + ':' + secs);
<input type="text" value="1:0:0" name="beforehr[]" />
<input type="text" value="1:0:0" name="beforehr[]" />
<br />
<input type="text" value="4:0:0" name="afterhr[]" />
<input type="text" value="4:0:0" name="afterhr[]" />

此外,请注意一元+运算符与parseInt相似(但其作用略有不同(。

~~只是Math.floor(number)的一种奇特表达方式

解决方案简化

<script>
function CalOt(){
Before=document.getElementsByName("beforehr[]");
After=document.getElementsByName("afterhr[]");
TodayOt=document.getElementsByName("txtTodayOt[]");
MonthTotal=0
for(i=0;i<Before.length;i++){
//alert(TimetoSec(Before[i].value));
BeforeInSeconds=TimetoSec(Before[i].value); //Convert Before[i].value to Seconds
AfterInSeconds=TimetoSec(After[i].value);//Convert After[i].value to Seconds
Daytot=parseInt(BeforeInSeconds)+parseInt(AfterInSeconds);
TodayOt[i].value=SecToTime(Daytot);
MonthTotal=parseInt(MonthTotal)+parseFloat(Daytot);
}
MonthTotalHRS=SecToTime(MonthTotal);// Convert MonthTotal value to Time
document.getElementById("txtMonthTotal").value=MonthTotal;
document.getElementById("Mthtotal").innerHTML=MonthTotalHRS;
}
function TimetoSec(Time){
TimeSplit=Time.split(":");
HoursSeconds=TimeSplit[0]*60*60;
Minutes=TimeSplit[1]*60;
TotalSec=parseFloat(HoursSeconds)+parseFloat(Minutes)+parseFloat(TimeSplit[2]);
console.log(TotalSec+"n");
return TotalSec;
}
function SecToTime(Seconds){
Hr=Math.floor(Seconds/(60*60));
Mn=Seconds % (60*60);
Min=Math.floor(Mn/(60));
Sec=Mn % (60);
return Hr+":"+Min+":"+Sec;
}
</script>