JavaScript - sumar y restar horas

 
Vista:

sumar y restar horas

Publicado por Karla (1 intervención) el 05/05/2005 02:24:05
Hola como puedo sumar horas
ejemplo

4:01
2:03
_____
6:04

Saludos
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

sumar y restar horas

Publicado por Luis Cruz ^_^ (1 intervención) el 06/03/2017 15:10:41
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<html>
    <head>
        <script language="JavaScript">
 
            function padNmb(nStr, nLen) {
                var sRes = String(nStr);
                var sCeros = "0000000000";
                return sCeros.substr(0, nLen - sRes.length) + sRes;
            }
 
            function stringToSeconds(tiempo) {
                var sep1 = tiempo.indexOf(":");
                var sep2 = tiempo.lastIndexOf(":");
                var hor = tiempo.substr(0, sep1);
                var min = tiempo.substr(sep1 + 1, sep2 - sep1 - 1);
                var sec = tiempo.substr(sep2 + 1);
                return (Number(sec) + (Number(min) * 60) + (Number(hor) * 3600));
            }
 
            function secondsToTime(secs) {
                var hor = Math.floor(secs / 3600);
                var min = Math.floor((secs - (hor * 3600)) / 60);
                var sec = secs - (hor * 3600) - (min * 60);
                return padNmb(hor, 2) + ":" + padNmb(min, 2) + ":" + padNmb(sec, 2);
            }
 
            function substractTimes(t1, t2) {
                var secs1 = stringToSeconds(t1);
                var secs2 = stringToSeconds(t2);
                var secsDif = secs1 + secs2;
                return secondsToTime(secsDif);
            }
 
            function calcT3() {
                with (document.frm)
                    t3.value = substractTimes(t1.value, t2.value);
            }
 
        </script>
    </head>
    <body>
        <form name="frm">
            Hora1 (hh:mm:ss): <input type="text" name="t1" value="12:30:15"><br>
            Hora2 (hh:mm:ss): <input type="text" name="t2" value="3:40:18"><br>
            <hr>
            Resta (hh:mm:ss): <input type="text" name="t3" value=""><br><br>
            <input type="button" onclick="calcT3()" value="Restar">
        </form>
    </body>
</html>
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar