在JavaScript中添加两个时间变量

在JavaScript中添加两个时间变量

我如何在JavaScript中添加两个时间变量。

例如1:

var a = 17.30hrs
var b=1.30hrs


因此总计应为(a+b) = 19.00hrs

示例2:

var a = 23:00hrs
var b = 1.30hrs


因此总计应为(a+b) = 00.30hrs

在这里,我们可以使用24 hrs time format

先谢谢您的帮助

最佳答案

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script language="javascript">
function getTimeTotal(time1,time2)
{
var time1_hr = "";
var time1_min = "";
var time2_hr = "";
var time2_min = "";
var total_hrtime = "";
var total_mintime = "";
var generated_Hour = 0;
    time1_hr = time1.split(":")[0];
    time1_min = time1.split(":")[1];
    time2_hr = time2.split(":")[0];
    time2_min = time2.split(":")[1];

    total_hrtime = 1* time1_hr + 1*time2_hr;

    alert(total_hrtime);
    total_mintime = 1* time1_min + 1*time2_min;
    alert(total_mintime);

    if(total_mintime>=60)
    {
        total_mintime = total_mintime - 60;
        total_hrtime = total_hrtime + 1;
    }

    if(total_hrtime>=24)
    {
        total_hrtime = total_hrtime -24 ;
                if(total_hrtime<10)
    total_hrtime = "0"+total_hrtime;

    }



alert(total_hrtime+":"+total_mintime);
}
</script>
</HEAD>

<BODY>
<input type="button" onClick="getTimeTotal('17:30','17:30');" />
</BODY>
</HTML>

09-25 18:15