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/
如果有人可以帮助我,谢谢你。
答案 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();