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

查看:17
本文介绍了来自两个字段的 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.

我想做的是获得两次之间的小时数.因此,例如,如果我的 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);

推荐答案

try

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

有一个小提琴

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

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