Hi, I am working on a form in php where I have the data of a person and I want to put the date of birth in dd / mm / yyyy format I automatically calculate the age and put it in the age field. Can this be done with javascript?
Hi, I am working on a form in php where I have the data of a person and I want to put the date of birth in dd / mm / yyyy format I automatically calculate the age and put it in the age field. Can this be done with javascript?
Yes, it can be done with JavaScript.
From this answer from Stack Overflow in English:
function calcularEdad(fecha) {
var hoy = new Date();
var cumpleanos = new Date(fecha);
var edad = hoy.getFullYear() - cumpleanos.getFullYear();
var m = hoy.getMonth() - cumpleanos.getMonth();
if (m < 0 || (m === 0 && hoy.getDate() < cumpleanos.getDate())) {
edad--;
}
return edad;
}
To put it in the desired field:
document.querySelector("#miCampo").textContent = calcularEdad(fecha);
It can be done with PHP much easier try this just make sure that the date of birth is a valid date with the format "year-month-day" and date is divided by the number of seconds of a calendar year
$fecha = time() - strtotime($nacimiento);
$edad = floor($fecha / 31556926);
This would have to give you the number of years of a person's birthday!
Greetings
Another option:
function calculateAge(birthday) {
var birthday_arr = birthday.split("/");
var birthday_date = new Date(birthday_arr[2], birthday_arr[1] - 1, birthday_arr[0]);
var ageDifMs = Date.now() - birthday_date.getTime();
var ageDate = new Date(ageDifMs);
return Math.abs(ageDate.getUTCFullYear() - 1970);
}
var age = calculateAge("15/04/1976");
alert( age );
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);
}
Try this function:
function calculate_age(birth_month, birth_day, birth_year) {
today_date = new Date();
today_year = today_date.getFullYear();
today_month = today_date.getMonth();
today_day = today_date.getDate();
age = today_year - birth_year;
if (today_month < (birth_month - 1)) {
age--;
}
if (((birth_month - 1) == today_month) && (today_day < birth_day)) {
age--;
}
console.log(age);
}