Foros del Web » Creando para Internet » HTML »

restar fechas

Estas en el tema de restar fechas en el foro de HTML en Foros del Web. Hola a todos, tengo un problema que no puedo solucionar al restar dos horas. <html> <head> <script language="JavaScript"> function mueveReloj(){ momentoActual = new Date() hora ...
  #1 (permalink)  
Antiguo 09/03/2012, 08:26
 
Fecha de Ingreso: marzo-2012
Mensajes: 2
Antigüedad: 12 años, 1 mes
Puntos: 0
Pregunta restar fechas

Hola a todos, tengo un problema que no puedo solucionar al restar dos horas.
<html>
<head>
<script language="JavaScript">
function mueveReloj(){
momentoActual = new Date()
hora = momentoActual.getHours()
minuto = momentoActual.getMinutes()
segundo = momentoActual.getSeconds()

horaImprimible = hora + " : " + minuto + " : " + segundo

document.info_repo.reloj1.value = horaImprimible

setTimeout("mueveReloj()",1000)
}
</script>
<script>
function copiar(fuente, destino) {
document.getElementById(destino).value= document.getElementById(fuente).value;
}
function desa(){
document.info_repo.b1.disabled=true;
document.info_repo.b2.disabled=false;
}
function desa1(){
document.info_repo.b2.disabled=true;
}
</script>
<head >
<body onload="mueveReloj()">
<form name="info_repo">
<label>Hora</label>
<input type="text" name="reloj1" id="hora1"/>
<input type="button" name="b1" value="hora inicio" onclick="copiar('hora1','hora2')+desa(this)"/>
<input type="text" name="" id="hora2"/>
<input type="button" name="b2" value="hora termino" disabled="true" onclick="copiar('hora1','hora3')+desa1(this)"/>
<input type="text" name="" id="hora3"/>
<input type="button" value="resta"/>
<input type="text"/>
</form>
</body>
</html>


cualquier comentario es aporte ....
  #2 (permalink)  
Antiguo 09/03/2012, 10:34
Avatar de emprear
Colaborador
 
Fecha de Ingreso: junio-2007
Ubicación: me mudé
Mensajes: 8.388
Antigüedad: 16 años, 10 meses
Puntos: 1567
Respuesta: restar fechas

En primer lugar, bienvenido a FDW @karlos_11

Aquí tenes un ejemplo funcional, comparalo con el tuyo

http://www.forosdelweb.com/wiki/Resta_de_horas


Saludos
__________________
La voz de las antenas va, sustituyendo a Dios.
Cuando finalice la mutación, nueva edad media habrá
S.R.
  #3 (permalink)  
Antiguo 11/03/2012, 13:21
 
Fecha de Ingreso: marzo-2012
Mensajes: 2
Antigüedad: 12 años, 1 mes
Puntos: 0
Respuesta: restar fechas

gracias por el ejemplo.......los mezcle y pude tener el resultado que buscaba
<html>
<head>
<script language="JavaScript">
function mueveReloj(){
momentoActual = new Date()
hora = momentoActual.getHours()
minuto = momentoActual.getMinutes()
segundo = momentoActual.getSeconds()

horaImprimible = hora + " : " + minuto + " : " + segundo

document.info_repo.reloj1.value = horaImprimible

setTimeout("mueveReloj()",1000)
}
</script>
<script>
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(h1, h2){
var secs1 = stringToSeconds(h1);
var secs2 = stringToSeconds(h2);
var secsDif = secs1 - secs2;
return secondsToTime(secsDif);
}
function copiar(fuente, destino) {
document.getElementById(destino).value= document.getElementById(fuente).value;
}
function desa(){
document.info_repo.b1.disabled=true;
document.info_repo.b2.disabled=false;
}
function desa1(){
document.info_repo.b2.disabled=true;
}
function calcT3(){
with (document.info_repo)
h3.value = substractTimes(h2.value, h1.value);
}
</script>
<head >
<body onload="mueveReloj()">
<form name="info_repo">
<label>Hora</label>
<input type="text" name="reloj1" id="hora1"/>
<hr>
<input type="button" name="b1" value="hora inicio" onclick="copiar('hora1','hora2')+desa(this)"/>
<input type="text" name="h1" id="hora2"/>
<hr>
<input type="button" name="b2" value="hora termino" disabled="true" onclick="copiar('hora1','hora3')+desa1(this)"/>
<input type="text" name="h2" id="hora3"/>
<hr>
<input type="button" value="resta" onclick="calcT3()"/>
<input type="text" name="h3"/>
</form>
</body>
</html>

dejo el ejemplo terminado ...
atte.karlos_11

Etiquetas: restar, formulario
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 00:25.