Created
May 11, 2023 23:19
-
-
Save alejoasotelo/6a6b44bea166908814cba13bc9717402 to your computer and use it in GitHub Desktop.
Sumar horas Toggle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Se le pasan las horas (String) como parámetro, le hace un split por salto de linea | |
* y se suman. | |
* Lo uso para exportar las horas de toggl y contabilizar las horas trabajadas. | |
*/ | |
function sumarHoras(horas) { | |
if (!Array.isArray(horas)) { | |
horas = horas.split('\n') | |
} | |
horas = horas.map(h => { | |
var [h, m, s] = h.split(':').map(i => parseInt(i)); | |
return {h, m, s} | |
}); | |
var sum = horas.reduce((acum, current, i) => { | |
return { | |
h: acum.h + current.h, | |
m: acum.m + current.m, | |
s: acum.s + current.s, | |
} | |
}, { | |
h: 0, m: 0, s: 0 | |
}); | |
var s = sum.s / 60; | |
var plusM = Math.floor(s); | |
s = sum.s - (plusM*60) | |
var m = plusM + sum.m | |
m = m / 60 | |
var plusH = Math.floor(m) | |
m = plusM + sum.m - (plusH*60) | |
var h = plusH + sum.h | |
var result = { | |
h: h, | |
m, | |
s | |
}; | |
return result.h + ':' + result.m + ':' + result.s | |
} | |
var horas = `1:06:09 | |
2:04:37 | |
0:48:40 | |
1:19:52` | |
sumarHoras(horas) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment