0

大家好!我为我的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/

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

4

2 回答 2

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);
于 2013-05-19T16:03:57.117 回答
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/

于 2013-05-19T16:30:51.843 回答