我怎样才能在javascript中添加两个时间变量。
例如:
var a = 17.30hrs
var b=1.30hrs
所以总计应为(a+b) = 19.00hrs
example2:
var a = 23:00hrs
var b = 1.30hrs
所以总计应为(a+b) = 00.30hrs
我们可以使用24 hrs time format
。
提前感谢您的帮助
答案 0 :(得分: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>
答案 1 :(得分:0)
// 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/
答案 2 :(得分: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);
答案 3 :(得分: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';
}
答案 4 :(得分: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/