如何计算24小时制两次时钟之间的时差 [英] How to work out time difference between two times on a 24 hour clock

查看:274
本文介绍了如何计算24小时制两次时钟之间的时差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对用JavaScript生成的某些代码有疑问.我想计算一个24小时时钟两次之间的差异.数据来自两个输入时间字段:

I have a problem with some code I have been producing in JavaScript. I want to calculate the difference between two times on a 24 hour clock. The data comes from two input time fields:

<input type="time" id="start" />
<input type="time" id="end" />

因此,时间在字符串00:00中,这对数字计算没有帮助.

Because of this the times come in a string 00:00, which doesn't help for number calculations.

我的解决方法是从头开始减去头.如果结束时间较长,则此方法非常有效,但是如果结束时间超过11:00(00:00),则结果为负数.如果结尾比起点低,我尝试将结果加24,但是我仍然得到负数.这似乎是一个愚蠢的问题,但我从不擅长数学.

The way I worked it out was to minus the start from the end. This works perfectly if the the end time is greater, however if the end time is past 11:00 (00:00), I end up with a negative number. I have tried adding 24 to the result if the end is lower than the start but I still get a negative number. This may seem like a dumb question but I was never that good at maths.

var numHours;
if(time_end < time_start){
    numHours = parseInt(t_e.substring(0,2)) - parseInt(t_s.substring(0,2)) + 24;
}else{
    numHours = parseInt(t_e.substring(0,2)) - parseInt(t_s.substring(0,2));
}

可能(肯定)有一种更好的方法可以做到这一点,但是我如何才能使它起作用.我也可以计算分钟数以获得更准确的时差.

There is probably (definitely) a better way of doing this but how can I get this to work. Also could I calculate the minutes as well to get more accurate time difference.

推荐答案

提供的解决方案无法有效地解决日期边界问题.所有这些都假设差异少于24小时.这意味着我们在23小时59分钟的开始和结束之间的差异上有一个上限,否则我们会对结果感到困惑.但是请记住,正如描述的那样,一个真实的用例是,一个事件开始于晚上11点,结束于凌晨1点(从23:00到1:00),并且相差2小时而不是22小时.

The solutions provided aren't accounting for the day boundary effectively. And all of this assumes the difference is less than 24 hours. Meaning that we have an upper boundary on the difference between start and end of 23 hours and 59 minutes, otherwise we are confused by the result. But remember that as described a real use case is that an event starts at 11pm and ends at 1am (from 23:00 to 1:00) and the difference is 2 hours NOT 22 hours.

function calculateTime(e) {
   var startTime = $('#start').val();
   var endTime = $('#end').val();

   var startTimeArray = startTime.split(":");
   var startInputHrs = parseInt(startTimeArray[0]);
   var startInputMins = parseInt(startTimeArray[1]);

   var endTimeArray = endTime.split(":");
   var endInputHrs = parseInt(endTimeArray[0]);
   var endInputMins = parseInt(endTimeArray[1]);

   var startMin = startInputHrs*60 + startInputMins;
   var endMin = endInputHrs*60 + endInputMins;

   var result;

   if (endMin < startMin) {
       var minutesPerDay = 24*60; 
       result = minutesPerDay - startMin;  // Minutes till midnight
       result += endMin; // Minutes in the next day
   } else {
      result = endMin - startMin;
   }

   var minutesElapsed = result % 60;
   var hoursElapsed = (result - minutesElapsed) / 60;

   alert ( "Elapsed Time : " + hoursElapsed + ":" + (minutesElapsed < 10 ?
            '0'+minutesElapsed : minutesElapsed) ) ;
}

我没有检查,但我相信您可以做到这一点,但我没有检查它:

And I didn't check, but I believe you could just do this, but I'm not checking it :

var result = endMin - startMin;
if (result < 0 ) result = (24*60) + result;

这篇关于如何计算24小时制两次时钟之间的时差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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