1

我如何在 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.

在此先感谢您的帮助

4

5 回答 5

1
<!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>
于 2012-11-17T11:01:49.190 回答
0

尝试这个,

var a = "17.30hrs" 
var b = "1.30hrs"

alert(addtime(a,b)); //output: 19.00hrs 

var a = "23.00hrs" 
var b = "1.30hrs"

alert(addtime(a,b)); //output: 0:30hrs

//Add two times
function addtime(start_time, end_time){
    var startArr = start_time.replace('hrs','',start_time).split('.');
    var endArr = end_time.replace('hrs','',end_time).split('.');

    var d = new Date();
    startArr[0] = (startArr[0]) ? parseInt(startArr[0], 10) : 0;
    startArr[1] = (startArr[1]) ? parseInt(startArr[1], 10) : 0;
    endArr[0] = (endArr[0]) ? parseInt(endArr[0], 10) : 0;
    endArr[1] = (endArr[1]) ? parseInt(endArr[1], 10) : 0;

    d.setHours(startArr[0] + endArr[0]);
    d.setMinutes(startArr[1] + endArr[1]);   

    var hours = d.getHours();
    var minutes = d.getMinutes();

    return hours+'.'+minutes+'hrs';
}​

演示:http: //jsfiddle.net/AXVPf/5/

于 2012-11-17T11:05:00.913 回答
0

这似乎有效,但前提是您使用小时和分钟:

var time1 = new Date('','','', 17, 30);
var time2 = new Date('','','', 01, 30);

var hours = time1.getHours() + time2.getHours();
var minutes = time1.getMinuess() + time2.getMinutes();

if ( minutes >= 60 ) 
{
    hours++;
    minutes -= 60;
}

if ( hours >= 24 ) 
{
    hours -= 24;
}

console.log ( hours );
console.log ( minutes );

在这里小提琴:http: //jsfiddle.net/VzL7e/

于 2012-11-17T11:06:25.507 回答
0

对于第一个例子,

var date = new Date();
date.setHours(17);
date.setMinutes(30);
date.setSeconds(0);

var newDate = new Date(date.getTime());
newDate.setSeconds(newDate.getSeconds() + 0);
newDate.setMinutes(newDate.getMinutes() + 30);
newDate.setHours(newDate.getHours() + 1);
于 2012-11-17T10:40:41.360 回答
0

使用这个 JavaScript

// Declare Variables
var a = new Date(0);
var b = new Date(0);
a.setHours(23);
b.setHours(1);
b.setMinutes(30);
c = a - b;

// For Hours
hours = c / 1000 / 60 / 60;
// For Minutes
mins = c / 1000 / 60;

网络上有很多可用的示例。看一下这个:

// Simple function to calculate time difference between 2 Javascript date objects
function get_time_difference(earlierDate,laterDate)
{
       var nTotalDiff = laterDate.getTime() - earlierDate.getTime();
       var oDiff = new Object();
 
       oDiff.days = Math.floor(nTotalDiff/1000/60/60/24);
       nTotalDiff -= oDiff.days*1000*60*60*24;
 
       oDiff.hours = Math.floor(nTotalDiff/1000/60/60);
       nTotalDiff -= oDiff.hours*1000*60*60;
 
       oDiff.minutes = Math.floor(nTotalDiff/1000/60);
       nTotalDiff -= oDiff.minutes*1000*60;
 
       oDiff.seconds = Math.floor(nTotalDiff/1000);
 
       return oDiff;
 
}

以上代码取自:http: //blogs.digitss.com/javascript/calculate-datetime-difference-simple-javascript-code-snippet/

于 2012-11-17T10:35:01.097 回答