1

JavaScriptで2つの時間変数を追加するにはどうすればよいですか.

たとえば 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

この 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;

利用可能な Web 上の多くの例があります。これをチェックしてください:

// 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 に答える
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 に答える