JavaScript - Restar 2 fechas y obtener edad en años.

 
Vista:
Imágen de perfil de Bladimir

Restar 2 fechas y obtener edad en años.

Publicado por Bladimir (5 intervenciones) el 16/03/2015 02:01:29
Hola a todos. Tengo un archivo html donde hay 3 inpu. En uno de ellos se carga la fecha actual cuando se carga la pagina en formato dd/mm/yyyy, en otro el usuario debe ingresar su fecha de nacimiento en formato dd/mm/yyyy. En el tercer input debe aparecer, al hacer click en el button, la edad en años. ¿Como se haria con javascript? El codigo es el siguiente:
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>RHM</title>
</head>
  <script type="text/javascript">
 window.onload = function(){
	 fecha = new Date();
	 texto = document.getElementById("fechaing");
	 texto.value = fecha.getDate() + "/" + (fecha.getMonth() + 1) + "/" + fecha.getFullYear();
	 }
 </script>
 <script type="text/javascript">
 function startTime(){
	  today = new Date;
	  h=today.getHours();
 }
 </script>
 
<body>
	 <form name="elegir" method="post" action"<?=$_SERVER['PHP_SELF']?>>
	  <fieldset>
	   <legend>
	    <b> Prueba: </b>	    
	   </legend>
	    
	    <p> <b>Fecha de nacimiento:</b> <input type="text" name="fechanac"/>
            <b>Edad:</b> <input type="text" name="edad"/>
	    <p> <b>Fecha de Ingreso: </b><input type="date" id="fechaing"> </p>
	</fieldset>	 
	 <p>	  
	   </p>
	 <p align="center"  ><input type="button" value="Restar" name="restar"/></p>
	 </form>
	 
</body>
</html>

Gracias por la ayuda.
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
Imágen de perfil de Bladimir

Restar 2 fechas y obtener edad en años.

Publicado por Bladimir (5 intervenciones) el 20/03/2015 02:41:38
Resolvi el asunto, tomando un poco de aqui y allá, de la siguiente manera:
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
51
52
53
54
55
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Campos de texto</title>
 
<script type="text/javascript">
function escribe() {
	fechaHoy = new Date();
	annoActual = fechaHoy.getFullYear();
	mesActual = fechaHoy.getMonth()+1;
	diaActual = fechaHoy.getDate();
 
    escribir = document.getElementById("caja")
				 Dianac = document.elegir.dianac.value
				 Mesnac = document.elegir.mesnac.value
				 Anonac = document.elegir.anonac.value
				 var Edad = annoActual-Anonac;
                if(mesActual<Mesnac){
				 Edad--;
                }
                if(mesActual==Mesnac){
                	if(Dianac<diaActual){
       				 Edad--;
                       }
 
                   }
                escribir.innerHTML = Edad;
				 }
window.onload = function(){
	 fecha = new Date();
	 texto = document.getElementById("fechaing");
	 texto.value = fecha.getDate() + "/" + (fecha.getMonth() + 1) + "/" + fecha.getFullYear();
	 }
 
</script>
</head>
<body>
<h1> Calcular edad</h1>
<div id="caja">
 
</div>
<form name="elegir" action="#" >
<h2>Fecha de nacimiento:</h2>
<p> Dia : <input name="dianac" type="date"/> </p>
<p>Mes: <input name="mesnac" type="date" /></p>
<p>A&ntilde;o <input name="anonac" type="date" /></p>
<h2>Fecha de ingreso:</h2>
<p> Fecha de hoy : <input name="fechaing" type="date" id="fechaing"/> </p>
<p><input name="ver" id="ver" type="button" value="ver" onclick="escribe()"/>
<p>Edad:<input name="fin" type="texto" id="caja"/></p>
</form>
 
</body>
</html>
El asunto es que no se como hacer para que la edad aparezca en el input "fin". ¿Alguna idea?
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

Restar 2 fechas y obtener edad en años.

Publicado por Efrain (1 intervención) el 11/07/2016 20:32:28
creo q es

document.getElementById("fechaing").value = Edad;
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

Restar 2 fechas y obtener edad en años.

Publicado por Anaya (1 intervención) el 21/04/2017 18:38:19
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
51
52
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Campos de texto</title>
 
<script type="text/javascript">
function escribe() {
  fechaHoy = new Date();
  annoActual = fechaHoy.getFullYear();
  mesActual = fechaHoy.getMonth()+1;
  diaActual = fechaHoy.getDate();
 
        escribir = document.getElementById("caja")
         Dianac = document.elegir.dianac.value
         Mesnac = document.elegir.mesnac.value
         Anonac = document.elegir.anonac.value
         var Edad = annoActual-Anonac;
                if(mesActual<Mesnac){
         Edad--;
                }
                if(mesActual==Mesnac){
                  if(Dianac<diaActual){
               Edad--;
                       }
 
                   }
                escribir.innerHTML = Edad;
   document.getElementById("fin").value = Edad;
   }
 
</script>
</head>
<body>
<h1> Calcular edad</h1>
<div id="caja">
 
</div>
<form name="elegir" action="#" >
<h2>Fecha de nacimiento:</h2>
<p> Dia : <input name="dianac" type="text"/> </p>
<p>Mes: <input name="mesnac" type="text" /></p>
<p>A&ntilde;o <input name="anonac" type="text" /></p>
<h2>Fecha de ingreso:</h2>
<p> Fecha de hoy : <input name="fechaing" type="date" id="fechaing"/> </p>
<p><input name="ver" id="ver" type="button" value="ver" onclick="escribe()"/>
<p>Edad:<input name="fin" type="text" id="fin"/></p>
</form>
 
</body>
</html>


ahi te indico como muestras el resultado dentro del input
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