Reputation: 3283
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
<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
Upvotes: 1
Views: 7314
Reputation: 10466
This is javascript, Second calculation also included
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/
Updated with second calculation
Upvotes: 4
Reputation: 10074
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.
Upvotes: 0
Reputation: 700342
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/
Upvotes: 10
Reputation: 20254
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);
Upvotes: 0
Reputation: 28511
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
Upvotes: 1