两个字段的Jquery时差以小时为单位 [英] Jquery time difference in hours from two fields

查看:211
本文介绍了两个字段的Jquery时差以小时为单位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表单中有两个字段,用户选择输入时间(start_time,end_time)我想在更改这些字段时重新计算另一个字段的值。

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.

我想做的是获得2次之间的小时数。因此,例如,如果我的start_time为5:30,结束时间为7:50,我想将结果2:33放入另一个字段。

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.

我输入的表格时间格式为HH:MM:SS

My inputted form times are in the format 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);


推荐答案

尝试

var diff = ( new Date("1970-1-1 " + end_time) - new Date("1970-1-1 " + start_time) ) / 1000 / 60 / 60;  

小提琴

这篇关于两个字段的Jquery时差以小时为单位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆