Looking to add time together within javascript.
I have never written any javascript, and as such am struggling with the way to progress.
I have got to add say 4:00
(4 hours) to 12:44
(Current Time) Is this possible within javascript?
The answer should report back 16:44
If so, how would I go about it?
Thanks
Looking to add time together within javascript.
I have never written any javascript, and as such am struggling with the way to progress.
I have got to add say 4:00
(4 hours) to 12:44
(Current Time) Is this possible within javascript?
The answer should report back 16:44
If so, how would I go about it?
Thanks
Share Improve this question edited Feb 22, 2023 at 19:16 ggorlen 57k8 gold badges110 silver badges150 bronze badges asked Sep 10, 2014 at 11:44 JoeMJoeM 931 gold badge3 silver badges13 bronze badges 4 |5 Answers
Reset to default 8If you break it down into a couple of small helper functions, it's not too hard:
// Convert a time in hh:mm format to minutes
function timeToMins(time) {
var b = time.split(':');
return b[0]*60 + +b[1];
}
// Convert minutes to a time in format hh:mm
// Returned value is in range 00 to 24 hrs
function timeFromMins(mins) {
function z(n){return (n<10? '0':'') + n;}
var h = (mins/60 |0) % 24;
var m = mins % 60;
return z(h) + ':' + z(m);
}
// Add two times in hh:mm format
function addTimes(t0, t1) {
return timeFromMins(timeToMins(t0) + timeToMins(t1));
}
console.log(addTimes('12:13', '01:42')); // 13:55
console.log(addTimes('12:13', '13:42')); // 01:55
console.log(addTimes('02:43', '03:42')); // 06:25
Take a look at moment.js - an excellent library for managing all kinds of time related functionality - momentjs.com
Later addition to answer:
You mention your are a newbie with JavaScript so here is a simple working example of your problem using moment.js - this example assumes the file and moment.js are in the same folder. Check out the docs on the moment.js for all the formatting options. Good luck.
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Add Time</title>
<script src="moment.js"></script>
</head>
<body>
<script>
//add 4 hours to the stated time
var theFutureTime = moment().hour('12').minute('44').add(4,'hours').format("HH:mm");
console.log(theFutureTime); // prints 16:44
</script>
</body>
Using moment.js
you can convert hh:mm
to minutes and add them.
Example:
moment.duration("02:45").asMinutes() + moment.duration("02:15").asMinutes()
Result: 300 mins
So, you can convert minutes to hh:mm
:
function timeConvert1(data) {
var minutes = data % 60;
var hours = (data – minutes) / 60;
return (hours + “:” + minutes);
}
For add array of time-format strings (including seconds and without counting days).
For example:
Input:
times = ['00:00:10', '00:24:00']
Output
00:24:10
// Add two times in hh:mm:ss format
function addTimes(times = []) {
const z = (n) => (n < 10 ? '0' : '') + n;
let hour = 0
let minute = 0
let second = 0
for (const time of times) {
const splited = time.split(':');
hour += parseInt(splited[0]);
minute += parseInt(splited[1])
second += parseInt(splited[2])
}
const seconds = second % 60
const minutes = parseInt(minute % 60) + parseInt(second / 60)
const hours = hour + parseInt(minute / 60)
return z(hours) + ':' + z(minutes) + ':' + z(seconds)
}
function addHours(start, end) {
var mins= moment.duration(start).asMinutes() + moment.duration(end).asMinutes();
function z(n){return (n<10? '0':'') + n;}
var h = (mins/60 |0) % 24;
var m = mins % 60;
return z(h) + ':' + z(m);
}
12:44
and you add 13 hours to it? – tuomassalo Commented Sep 10, 2014 at 11:49