简体   繁体   English

从另一个减去一次

[英]subtracting one time from another

I have had a good search for what I'm looking for but can't seem to find exactly what I need. 我已经很好地寻找了我正在寻找的东西,但似乎无法找到我需要的东西。 I'm new to jQuery and JavaScript. 我是jQuery和JavaScript的新手。

All I want to do is subtract one 24 hour clock time from another to find how many hours have been allocated using JQuery or JavaScript ie start time: 12:00 end time: 16:00 would be 4 hours. 我想做的就是从另一个时钟中减去一个24小时的时钟,以查找使用JQuery或JavaScript分配了多少小时,即开始时间:12:00结束时间:16:00将是4小时。

How would I go about doing this without having issues when going from say 12:00 till 12:00 the following day? 从第二天的12:00到12:00,如何在没有问题的情况下进行此操作?

I am not dealing with with dates, just time. 我不是在处理日期,只是时间。

Currently the times are stored like this as part of an object with start_time end_time : 目前,时间以这样的形式存储为具有start_time end_time的对象的一部分:

var shift = {'location':$('#shift_location').val(),
             'shift_date': $('#shift_date').val(),
             'start_time':$('#shift_start_time').val(),
             'end_time':$('#shift_end_time').val()
            };
var shift_list = JSON.parse(localStorage.shift);
shift_list.push(shift);
localStorage.shift = JSON.stringify(shift_list);  

You can use momentjs to do things with dates in javascript. 您可以使用momentjs在javascript中使用日期执行操作。

Example ( moment doc , fiddle ): 示例( 时刻doc小提琴 ):

var start_time = "12:00";
var end_time = "16:00";

var start = moment.duration(start_time, 'h');
var end = moment.duration(end_time, 'h');
alert(end.subtract(start).hours()); // 4

Of course, because of the simplicity of the task you could always use plain javascript: 当然,由于任务的简单性,你总是可以使用普通的javascript:

var start_time = "12:00";
var end_time = "16:00";
alert(parseInt(end_time, 10) - parseInt(start_time, 10)); // 4

With the given information ie start_time and end_time there is no way you can cover multiple days. 使用给定的信息,即start_time和end_time,您无法覆盖多天。 They just oscillate between 0 to 23 hours. 它们只需在0到23小时之间振荡。 There is no counter involved to calculate multiple days. 计算多天没有计数器。 if you need that you need two more states which will store start_date and end_date which will act as counter as pointed by @John Boker. 如果你需要,你需要另外两个状态,它们将存储start_date和end_date,它们将作为@John Boker所指出的反击。 But if you are sure that the difference never goes beyond 24 hours then we can use the parseTime from JAVASCRIPT: subtracting Time and getting its number of minutes function with our own little modifications. 但是如果你确定差异永远不会超过24小时,那么我们可以使用来自JAVASCRIPT的parseTime :减去时间并通过我们自己的少量修改获得其分钟数

function parseTime(s) {
   var c = s.split(':');
   return parseInt(c[0]) * 60 + parseInt(c[1]);
}

var limit = parseTime("23:59");

function getDiff(start_time, end_time){
   var a = parseTime(start_time), b = parseTime(end_time);
   if(b < a) // means its the next day.
      return Math.round((limit - a + b)/60);
   else if(b > a)
      return Math.round((b - a)/60);
   else if(b - a == 0)
      return 24.0;
   else
      alert("Invalid data");
 }

 alert(getDiff("12:00", "11:00"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM