Calculate age with a builder object in Javascript

2

I have the following problem:

How to calculate the age of a person with a constructor object that I lack?

function calcularEdad(nombre, diaNacimiento, mesNacimiento, anioNacimiento) {
 var obj={
     nombre:nombre,
     diaNacimiento:diaNacimiento,
     mesNacimiento:mesNacimiento,
     anioNacimiento:anioNacimiento
 }

var fecha_hoy = new Date();
var ahora_ano = fecha_hoy.getYear();
var ahora_mes = fecha_hoy.getMonth();
var ahora_dia = fecha_hoy.getDate();
var edad = (ahora_ano + 1900) - ano;

if ( ahora_mes < ( - 1)){
  edad--;
}
if (((mesNacimiento - 1) == ahora_mes) && (ahora_dia < diaNacimiento)){ 
  edad--;
}
if (edad > 2017){
    edad -= 2017;
}

return "¡Tienes " + edad + " años!";
}
    
asked by Marcos Crispino 10.01.2017 в 19:13
source

2 answers

1

You have had some failures in the conditions when calculating the age and to receive the value of an object you have to make reference, ex: obj.nombre

Your code working:

function calcularEdad(nombre, diaNacimiento, mesNacimiento, anioNacimiento) {
  
  var obj = {
    nombre:nombre,
    diaNacimiento:diaNacimiento,
    mesNacimiento:mesNacimiento,
    anioNacimiento:anioNacimiento
  }

  var fecha_hoy = new Date();
  var ahora_ano = fecha_hoy.getFullYear();
  var ahora_mes = fecha_hoy.getMonth();
  var ahora_dia = fecha_hoy.getDate();
  
  var edad = ahora_ano - obj.anioNacimiento;
  
  if ( ahora_mes < (obj.mesNacimiento - 1)) {
    
    edad--;    
  }
    
  if (((obj.mesNacimiento - 1) == ahora_mes) && (ahora_dia < obj.diaNacimiento)) {
        
    edad--;
  }

  return obj.nombre + ", tienes " + edad + " años!";
}


alert(calcularEdad('Sammia', 28, 8, 1998));
    
answered by 10.01.2017 в 19:51
-1

get the age (years, months and days) to from the date of birth with javascript

Age calculation function (years, months and days)

function calcularEdad(fecha) {
        // Si la fecha es correcta, calculamos la edad

        if (typeof fecha != "string" && fecha && esNumero(fecha.getTime())) {
            fecha = formatDate(fecha, "yyyy-MM-dd");
        }

        var values = fecha.split("-");
        var dia = values[2];
        var mes = values[1];
        var ano = values[0];

        // cogemos los valores actuales
        var fecha_hoy = new Date();
        var ahora_ano = fecha_hoy.getYear();
        var ahora_mes = fecha_hoy.getMonth() + 1;
        var ahora_dia = fecha_hoy.getDate();

        // realizamos el calculo
        var edad = (ahora_ano + 1900) - ano;
        if (ahora_mes < mes) {
            edad--;
        }
        if ((mes == ahora_mes) && (ahora_dia < dia)) {
            edad--;
        }
        if (edad > 1900) {
            edad -= 1900;
        }

        // calculamos los meses
        var meses = 0;

        if (ahora_mes > mes && dia > ahora_dia)
            meses = ahora_mes - mes - 1;
        else if (ahora_mes > mes)
            meses = ahora_mes - mes
        if (ahora_mes < mes && dia < ahora_dia)
            meses = 12 - (mes - ahora_mes);
        else if (ahora_mes < mes)
            meses = 12 - (mes - ahora_mes + 1);
        if (ahora_mes == mes && dia > ahora_dia)
            meses = 11;

        // calculamos los dias
        var dias = 0;
        if (ahora_dia > dia)
            dias = ahora_dia - dia;
        if (ahora_dia < dia) {
            ultimoDiaMes = new Date(ahora_ano, ahora_mes - 1, 0);
            dias = ultimoDiaMes.getDate() - (dia - ahora_dia);
        }

        return edad + " años, " + meses + " meses y " + dias + " días";
    }

Function isNumber

 function esNumero(strNumber) {
        if (strNumber == null) return false;
        if (strNumber == undefined) return false;
        if (typeof strNumber === "number" && !isNaN(strNumber)) return true;
        if (strNumber == "") return false;
        if (strNumber === "") return false;
        var psInt, psFloat;
        psInt = parseInt(strNumber);
        psFloat = parseFloat(strNumber);
        return !isNaN(strNumber) && !isNaN(psFloat);
    }
    
answered by 10.01.2017 в 19:23