简体   繁体   English

在没有Date对象的javascript中为时间添加数字

[英]Add number to time in javascript without Date object

I am trying to add number of hours to time. 我正在尝试增加小时数。 I am not using Date object nor I want to. 我不使用Date对象,也不想要。

Suppose I select 4 hours from below drop down:- 假设我从下拉菜单中选择4小时:

<select class="form-control" id="hr-sel" name="hour" required>
  <option value="" selected>Decide Hour</option>
  <option value="4">4 |  550</option>
  <option value="6">6 | 750</option>
  <option value="9">9 | 1000</option>
  <option value="12">12 | 1200</option>
</select>

And select 3:00 from below from input type date. 然后从输入类型日期的下方选择3:00。

<input class="form-control" placeholder="Pick Time" type="text" id="pick_time" name="pick" onfocus="(this.type='time')" onfocusout="(this.type='text')"; onchange="make_call();" onblur="setDropTime(hr,this.value);" required>

I want below form to be auto-updated by 7:00. 我希望下面的表格在7:00之前自动更新。

<input class="form-control" id="drop_time" name="drop" placeholder="Drop Time" type="text" onfocus="(this.type='time')" onfocusout="(this.type='text')"; required>

But it merges both the values. 但是它将两个值合并。 Like 4 hours and 3:00 gets me 43. 像4时3:00使我43。

Below is the JS function which I tried to. 下面是我尝试过的JS函数。 Kindly help me out. 请帮我。

function setDropTime(hours, pick_time) {
   var hours = document.forms["book"]["hr-sel"].value;
    sum = hours + pick_time;

  return sum;
}

Kindly Help me out please. 请帮助我。

First, we'll need to parse the displayed item. 首先,我们需要解析显示的项目。 It will be easiest for us to folow if we do our maths in minutes. 如果我们在几分钟内完成数学运算,那么对我们来说最容易做到。

In order to keep overview, and not repeat ourselves, make a function that can parse the time strings into a number of minutes 为了保持概述而不重复自己,创建一个可以将时间字符串解析为几分钟的函数

function parseTimeToMinutes(timeValue)
{
    //we split the input on the ':' symbol, so we get an arrray
    //with the hours and minutes (still as strings)
    var separated_values = timeValue.split(':');
    var num_hours = parseInt(separated_values[0]);
    var num_minutes = parseInt(separated_values[1]);

    //figure out the total number of minutes so we can do our math
    var sum_minutes = num_minutes + (num_hours * 60);
    return sum_minutes;
}

we will also need a function to format back from our number of minutes to a string. 我们还需要一个函数来将分钟数格式化回字符串。

function formatAsTime(numericValue)
{
    var restingMinutes = numericValue % 60;
    var hours = Math.floor(numericValue / 60);
    return hours + ":" + ("0" + restingMinutes).slice(-2);
}

you can then do this 然后您可以执行此操作

//please note : your code looks very confusing at this point,
//i'm not entirely sure this is how your form works.
//so your mileage may vary. use this as a guiding example : 
function setDropTime() {
     var originalTime = parseTimeToMinutes(document.getElementById('Pick Time').value);
     var timeToAdd = parseTimeToMinutes(document.getElementById('drop_time').value));

     var minutesTotal = originalTime + timeToAdd;

     return formatAsTime(minutesTotal);
}

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

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