Jquery 2小时之间的差异

时间:2013-05-19 16:00:34

标签: jquery datetime date-difference

大家好!我发现这个脚本用于我http://jsfiddle.net/diode/nuv7t/5/的项目,它在2小时之间产生差异,但我需要定义这2个变量

var start_actual_time  =  "01/17/2012 11:20";
var end_actual_time    =  "01/18/2012 12:25";

像这样:

var start_actual_time  =  "input#start";
var end_actual_time    =  "input#end";

和写入新输入的差异#result

以下是我尝试的内容:http://jsfiddle.net/UmwXW/

如果有人可以帮助我,谢谢你。

2 个答案:

答案 0 :(得分:6)

演示--> http://jsfiddle.net/UmwXW/1/

var start_actual_time = $("#start").val();
var end_actual_time = $("#end").val();

start_actual_time = new Date(start_actual_time);
end_actual_time = new Date(end_actual_time);

var diff = end_actual_time - start_actual_time;

var diffSeconds = diff / 1000;
var HH = Math.floor(diffSeconds / 3600);
var MM = Math.floor(diffSeconds % 3600) / 60;

var formatted = ((HH < 10) ? ("0" + HH) : HH) + ":" + ((MM < 10) ? ("0" + MM) : MM)
$('#result').val(formatted);

答案 1 :(得分:0)

这是我使用keyup函数的最终代码。谢谢pXL。

$("#end").keyup(function () {


var start_actual_time = $("#start").val();
    var end_actual_time = $("#end").val();

    start_actual_time = new Date(start_actual_time);
    end_actual_time = new Date(end_actual_time);

    var diff = end_actual_time - start_actual_time;

    var diffSeconds = diff / 1000;
    var HH = Math.floor(diffSeconds / 3600);
    var MM = Math.floor(diffSeconds % 3600) / 60;

    var formatted = ((HH < 10) ? ("0" + HH) : HH) + ":" + ((MM < 10) ? ("0" + MM) : MM)
   $('#result').val(formatted);

      }).keyup();

http://jsfiddle.net/UmwXW/2/