You are on page 1of 2

<html>

<head>
<title>calcula edad aproximada en d�as meses y a�os</title>
<script language="javascript">
function calcula(formulario) {
var xnac = formulario.fnac.value;
var xhoy = formulario.fhoy.value;
var partesnac=xnac.split("/");
var parteshoy=xhoy.split("/");
// conversi�n a n�meros
dhoy=parteshoy[0]*1;
mhoy=parteshoy[1]*1;
ahoy=parteshoy[2]*1; //otra forma de convertir
dnac=number(partesnac[0]);
mnac=number(partesnac[1]);
anac=number(partesnac[2]);

if (dnac > dhoy){


dhoy= dhoy + 30;
mhoy= mhoy - 1;

}
if (mnac > mhoy){
mhoy= mhoy +12;
ahoy= ahoy - 1;
}
dd=dhoy - dnac
mm=mhoy - mnac;
aa = ahoy - anac;
alert("dias:"+dd+"\n meses:"+ mm+"\n a�os:"+aa);

publica(dd,mm,aa);
}
function publica(dd,mm,aa) {
var raiz = document.documentelement;
var cuerpo = raiz.getelementsbytagname("body")[0];
nuevodiv=document.createelement('div');
cuerpo.appendchild(nuevodiv);

nuevodiv.style.backgroundcolor='#ddccaa';
nuevodiv.style.border='outset';
nuevodiv.style.width='200px';
nuevodiv.style.padding='5px';

texto="dias:"+dd+"<br />meses:"+mm+"<br />a�os:"+aa;


text1 = document.createtextnode("su edad es:");
nuevoh=document.createelement('h2');
nuevodiv.appendchild(nuevoh);
nuevoh.appendchild(text1);

nuevop=document.createelement('p');
nuevodiv.appendchild(nuevop);
nuevop.innerhtml=texto;
nuevop.style.padding='25px';
}
</script>
</head>
<body>
<form name="form1" >
<fieldset>
<legend>c�lculo de su edad exacta</legend>
<table border=0>
<tr>
<td>fecha de nacimiento:</td><td><input type="text" name="fnac"
value="dd/mm/aaaa" /></td>
</tr>
<tr>
<td>fecha actual:</td><td><input type="text" name="fhoy" value="dd/mm/aaaa"
/></td>
<td><input type="button" name="buton" value="calcula edad"
onclick="calcula(this.form)" /></td>
</tr>
</table>
</fieldset>
</form>

</body>
</html>

You might also like