Javascript 来自两个字段的 Jquery 时差(以小时为单位)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11883768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery time difference in hours from two fields
提问by jenson-button-event
I have two fields in my form where users select an input time (start_time, end_time) I would like to, on the change of these fields, recalcuate the value for another field.
我的表单中有两个字段,用户可以在其中选择一个输入时间(start_time、end_time),我希望在更改这些字段时重新计算另一个字段的值。
What I would like to do is get the amount of hours between 2 times. So for instance if I have a start_time of 5:30 and an end time of 7:50, I would like to put the result 2:33 into another field.
我想做的是获得两次之间的小时数。因此,例如,如果我的 start_time 为 5:30,结束时间为 7:50,我想将结果 2:33 放入另一个字段。
My inputted form times are in the format HH:MM:SS
我输入的表单时间格式为 HH:MM:SS
So far I have tried...
到目前为止,我已经尝试...
$('#start_time,#end_time').on('change',function()
{
var start_time = $('#start_time').val();
var end_time = $('#end_time').val();
var diff = new Date(end_time) - new Date( start_time);
$('#setup_hours').val(diff);
回答by jenson-button-event
try
尝试
var diff = ( new Date("1970-1-1 " + end_time) - new Date("1970-1-1 " + start_time) ) / 1000 / 60 / 60;
回答by Nick
It depends on what format you want your output in. When doing math with Date objects, it converts them into milliseconds since Epoch time (January 1, 1970, 00:00:00 UTC). By subtracting the two (and taking absolute value if you don't know which is greater) you get the raw number of millisecondsbetween the two.
这取决于您希望输出的格式。使用 Date 对象进行数学运算时,它会将它们转换为自纪元时间(UTC 时间 1970 年 1 月 1 日 00:00:00)以来的毫秒数。通过减去两者(如果您不知道哪个更大,则取绝对值),您可以获得两者之间的原始毫秒数。
From there, you can convert it into whatever format you want. To get the number of seconds, just divide that number by 1000
. To get hours, minutes, and seconds:
从那里,您可以将其转换为您想要的任何格式。要获得秒数,只需将该数字除以1000
。获取小时、分钟和秒:
var diff = Math.abs(new Date(end_time) - new Date(start_time));
var seconds = Math.floor(diff/1000); //ignore any left over units smaller than a second
var minutes = Math.floor(seconds/60);
seconds = seconds % 60;
var hours = Math.floor(minutes/60);
minutes = minutes % 60;
alert("Diff = " + hours + ":" + minutes + ":" + seconds);
You could of course make this smarter with some conditionals, but this is just to show you that using math you can format it in whatever form you want. Just keep in mind that a Date object always has a date, not just a time, so you can store this in a Date object but if it is greater than 24 hours you will end up with information not really representing a "distance" between the two.
当然,您可以使用一些条件使其更智能,但这只是为了向您展示使用数学可以将其格式化为您想要的任何形式。请记住,Date 对象始终具有日期,而不仅仅是时间,因此您可以将其存储在 Date 对象中,但如果它大于 24 小时,您最终会得到的信息并不真正代表两个对象之间的“距离”。二。
回答by Clyde Lobo
var start = '5:30';
var end = '7:50';
s = start.split(':');
e = end.split(':');
min = e[1]-s[1];
hour_carry = 0;
if(min < 0){
min += 60;
hour_carry += 1;
}
hour = e[0]-s[0]-hour_carry;
min = ((min/60)*100).toString()
diff = hour + ":" + min.substring(0,2);
alert(diff);
回答by Shreedhar
try this :
尝试这个 :
var diff = new Date("Aug 08 2012 9:30") - new Date("Aug 08 2012 5:30");
diff_time = diff/(60*60*1000);