如何添加小时,分钟和秒?

时间:2021-01-24 02:47:37

I have two times in_time and out_time.I want to add these two times using javascript.How can I do this? The value of in_time and out_time are coming from the text box. My html code is

我有两次in_time和out_time。我想用javascript添加这两次。我怎么能这样做? in_time和out_time的值来自文本框。我的HTML代码是

<div>   
<b>PunchIn</b>
    <input type="text" id="intime" class="time" placeholder="09:00:00" />
    <p class="error">PunchTime is not valid</p>
</div>
<br/>
<div>   
    <b>OutTime</b>
    <input type="text" id="brktime" class="time" placeholder="06:00:00" />
    <p class="error1">OutTime is not valid</p>
</div>

You can see the code from Demo

您可以从Demo中看到代码

5 个解决方案

#1


3  

This is javascript, Second calculation also included

这是javascript,还包括第二次计算

Try this,

尝试这个,

var a = "17.30.15hrs"
var b = "1.30.20hrs"

alert(addtime(a, b));

var a = "23.00.25hrs"
var b = "1.30.60hrs"

alert(addtime(a, b));

function addtime(start_time, end_time) {
    var startArr = start_time.replace('hrs', '', start_time).split('.');
    var endArr = end_time.replace('hrs', '', end_time).split('.');

    var d = new Date();
    startArr[0] = (startArr[0]) ? parseInt(startArr[0], 10) : 0;
    startArr[1] = (startArr[1]) ? parseInt(startArr[1], 10) : 0;
    startArr[2] = (startArr[2]) ? parseInt(startArr[2], 10) : 0;
    endArr[0] = (endArr[0]) ? parseInt(endArr[0], 10) : 0;
    endArr[1] = (endArr[1]) ? parseInt(endArr[1], 10) : 0;
    endArr[2] = (endArr[2]) ? parseInt(endArr[2], 10) : 0;

    d.setHours(startArr[0] + endArr[0]);
    d.setMinutes(startArr[1] + endArr[1]);
    d.setSeconds(startArr[2] + endArr[2]);

    var hours = d.getHours();
    var minutes = d.getMinutes();
    var seconds = d.getSeconds();

    return hours + '.' + minutes + '.' + seconds + 'hrs';
}

Demo: http://jsfiddle.net/AXVPf/7/

演示:http://jsfiddle.net/AXVPf/7/

Updated with second calculation

更新了第二次计算

#2


10  

Convert the times to seconds, add them, and format the result:

将时间转换为秒,添加它们并格式化结果:

function toSeconds(s) {
  var p = s.split(':');
  return parseInt(p[0], 10) * 3600 + parseInt(p[1], 10) * 60 + parseInt(p[2], 10);
}

function fill(s, digits) {
  s = s.toString();
  while (s.length < digits) s = '0' + s;
  return s;
}

var sec = toSeconds(intime) + toSeconds(out);

var result =
  fill(Math.floor(sec / 3600), 2) + ':' +
  fill(Math.floor(sec / 60) % 60, 2) + ':' +
  fill(sec % 60, 2);

Demo: http://jsfiddle.net/Guffa/JLh6W/

演示:http://jsfiddle.net/Guffa/JLh6W/

#3


1  

https://github.com/timrwood/moment The minutes library covers most of the things you need. Check out the Google Closure Library date implementation as well. http://closure-library.googlecode.com/svn/docs/namespace_goog_date.html

https://github.com/timrwood/moment分钟库涵盖了您需要的大部分内容。查看Google Closure Library日期实施。 http://closure-library.googlecode.com/svn/docs/namespace_goog_date.html

#4


0  

You can use the function below to convert your time strings into numbers, which you can then just add together

您可以使用下面的函数将时间字符串转换为数字,然后您可以将它们一起添加

function getTimeAsSeconds(time){
    var timeArray = time.split(':');
    return Number(timeArray [0]) * 3600 + Number(timeArray [1]) * 60 + Number(timeArray[2]);
}

Something like:

就像是:

var timeInSeconds = getTimeAsSeconds(document.getElementById('intime').value) + getTimeAsSeconds(document.getElementById('brktime').value);

#5


0  

There are many libraries out there (didn't do a google search, but @alex23 has one). However, the process is usually the same. Convert the strings to least common denominator. In this case seconds. Once you have the seconds add the two together then convert it back to hours minutes seconds again. These conversions are simple multiplications and devisions as @codebox demonstrated.

那里有很多图书馆(没有谷歌搜索,但@ alex23有一个)。但是,这个过程通常是一样的。将字符串转换为最小公分母。在这种情况下秒。一旦你有秒,将两者加在一起,然后再将其转换回小时分钟。这些转换是@codebox演示的简单乘法和设计。

#1


3  

This is javascript, Second calculation also included

这是javascript,还包括第二次计算

Try this,

尝试这个,

var a = "17.30.15hrs"
var b = "1.30.20hrs"

alert(addtime(a, b));

var a = "23.00.25hrs"
var b = "1.30.60hrs"

alert(addtime(a, b));

function addtime(start_time, end_time) {
    var startArr = start_time.replace('hrs', '', start_time).split('.');
    var endArr = end_time.replace('hrs', '', end_time).split('.');

    var d = new Date();
    startArr[0] = (startArr[0]) ? parseInt(startArr[0], 10) : 0;
    startArr[1] = (startArr[1]) ? parseInt(startArr[1], 10) : 0;
    startArr[2] = (startArr[2]) ? parseInt(startArr[2], 10) : 0;
    endArr[0] = (endArr[0]) ? parseInt(endArr[0], 10) : 0;
    endArr[1] = (endArr[1]) ? parseInt(endArr[1], 10) : 0;
    endArr[2] = (endArr[2]) ? parseInt(endArr[2], 10) : 0;

    d.setHours(startArr[0] + endArr[0]);
    d.setMinutes(startArr[1] + endArr[1]);
    d.setSeconds(startArr[2] + endArr[2]);

    var hours = d.getHours();
    var minutes = d.getMinutes();
    var seconds = d.getSeconds();

    return hours + '.' + minutes + '.' + seconds + 'hrs';
}

Demo: http://jsfiddle.net/AXVPf/7/

演示:http://jsfiddle.net/AXVPf/7/

Updated with second calculation

更新了第二次计算

#2


10  

Convert the times to seconds, add them, and format the result:

将时间转换为秒,添加它们并格式化结果:

function toSeconds(s) {
  var p = s.split(':');
  return parseInt(p[0], 10) * 3600 + parseInt(p[1], 10) * 60 + parseInt(p[2], 10);
}

function fill(s, digits) {
  s = s.toString();
  while (s.length < digits) s = '0' + s;
  return s;
}

var sec = toSeconds(intime) + toSeconds(out);

var result =
  fill(Math.floor(sec / 3600), 2) + ':' +
  fill(Math.floor(sec / 60) % 60, 2) + ':' +
  fill(sec % 60, 2);

Demo: http://jsfiddle.net/Guffa/JLh6W/

演示:http://jsfiddle.net/Guffa/JLh6W/

#3


1  

https://github.com/timrwood/moment The minutes library covers most of the things you need. Check out the Google Closure Library date implementation as well. http://closure-library.googlecode.com/svn/docs/namespace_goog_date.html

https://github.com/timrwood/moment分钟库涵盖了您需要的大部分内容。查看Google Closure Library日期实施。 http://closure-library.googlecode.com/svn/docs/namespace_goog_date.html

#4


0  

You can use the function below to convert your time strings into numbers, which you can then just add together

您可以使用下面的函数将时间字符串转换为数字,然后您可以将它们一起添加

function getTimeAsSeconds(time){
    var timeArray = time.split(':');
    return Number(timeArray [0]) * 3600 + Number(timeArray [1]) * 60 + Number(timeArray[2]);
}

Something like:

就像是:

var timeInSeconds = getTimeAsSeconds(document.getElementById('intime').value) + getTimeAsSeconds(document.getElementById('brktime').value);

#5


0  

There are many libraries out there (didn't do a google search, but @alex23 has one). However, the process is usually the same. Convert the strings to least common denominator. In this case seconds. Once you have the seconds add the two together then convert it back to hours minutes seconds again. These conversions are simple multiplications and devisions as @codebox demonstrated.

那里有很多图书馆(没有谷歌搜索,但@ alex23有一个)。但是,这个过程通常是一样的。将字符串转换为最小公分母。在这种情况下秒。一旦你有秒,将两者加在一起,然后再将其转换回小时分钟。这些转换是@codebox演示的简单乘法和设计。