0

我有两次获取时差的代码

        var starthours = document.getElementById("time3").value;
        var endhours = document.getElementById("time4").value;
        start = starthours.split(".");
        end = endhours.split(".");
        var startDate = new Date(0, 0, 0, start[0], start[1], 0);
        var endDate = new Date(0, 0, 0, end[0], end[1], 0);
        var diff = endDate.getTime() - startDate.getTime();
        var hours = Math.floor(diff / 1000 / 60 / 60);
        diff -= hours * 1000 * 60 * 60;
        var minutes = Math.floor(diff / 1000 / 60);
    document.getElementById("hourdiff").value = (hours < 9 ? "0" : "") + hours + "." + (minutes < 9 ? "0" : "") + minutes;

但是现在我必须为此结果添加另一个时间字段,我使用此代码获得该值

var timetv = document.getElementById("timetv").value;

And I want to add this to above time difference how to do that, Please help me.. 

Start time = 10.30
End time = 12.30
Time TV = 01.15

Resualt = (End Time - Start time) + Time TV 

答案应该是 = 3.15

4

3 回答 3

0

尝试这个,

var starthours = document.getElementById("time3").value;
var endhours = document.getElementById("time4").value;
var timetv = document.getElementById("timetv").value;

start = starthours.split(".");
end = endhours.split(".");
tvtime = timetv.split(".");

var startDate = new Date(0, 0, 0, start[0], start[1], 0);
var endDate = new Date(0, 0, 0, end[0], end[1], 0);

var diff = endDate.getTime() - startDate.getTime();

var hours = Math.floor(diff / 1000 / 60 / 60);
diff -= hours * 1000 * 60 * 60;
var minutes = Math.floor(diff / 1000 / 60);    

hours = hours + parseInt(tvtime[0]);
minutes = minutes + parseInt(tvtime[1]);
于 2013-08-12T06:21:09.337 回答
0

我不明白,你的问题是什么。

var d = new Date(
    new Date(0, 0, 0, 12, 30) -
    new Date(0, 0, 0, 10, 30) +
    (+new Date(0, 0, 0, 1, 15)) // transform Date into timestamp
);
[d.getHours(), d.getMinutes()]; // [3, 15]

另外,我通常更换

(hours < 9 ? "0" : "") + hours

("0" + hours).slice(-2)
于 2013-08-12T08:34:35.167 回答
0

当您已经从表单中将日期解析为 Date 对象时,这是为了获取值。

http://jsfiddle.net/gLReS/

var time1 = new Date('2013/08/12 10:30');
var time2 = new Date('2013/08/12 12:30');
var time3 = new Date('2013/08/12 1:15');

var result = (time2.getTime() - time1.getTime()) + time3.getTime();
var resultTime = new Date(result);
alert(resultTime);

然而,另一件事是获取这些对象,这取决于您的日期格式。

于 2013-08-12T06:44:47.607 回答