我下面有我的代码,
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
答案 0 :(得分:1)
假定变量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);
答案 1 :(得分:0)
您可以使用.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)
的一种奇特方式
答案 2 :(得分:0)
简化的解决方案
void VideosInsertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)
{
switch (progress.Status)
{
case UploadStatus.Uploading:
UpdateStatus(String.Format("{0} bytes sent.", progress.BytesSent));
break;
case UploadStatus.Failed:
UpdateStatus(String.Format("An error prevented the upload from completing.{0}", progress.Exception));
break;
}
}
void VideosInsertRequest_ResponseReceived(Video video)
{
UpdateStatus(string.Format("Video id '{0}' was successfully uploaded.", video.Id));
}
private void UpdateStatus(string status)
{
StatusLabel.Dispatcher.BeginInvoke(new Action(() => { StatusLabel.Content = status; }));
}