[英]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;
我需要將“小時前”轉換為秒,將“小時后”轉換為秒,將所有秒數相加,然后轉換為“時間”並將其放入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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.