# How to calculate the age of a person with JS, from the date of birth?

30

How can I calculate the age of a person in Javascript from the date of birth?

I tried something like:

``````function idade(dia, mes, ano) {
return new Date().getFullYear() - ano;
}

``````

asked by anonymous 11.02.2014 / 07:11

36

- Pedro : Hi, Maria. How old are you?

- Maria : Pedro, I was born on February 28, 1990.

- Pedro : So, I already know how old you are!

- Maria : What is it?

- Pedro : Just pick up the current year and subtract for the year you were born. Soon, 2014 - 1990 = 24. You are 24 years old.

- Maria : No, actually I'm 23 years old. I do 24 February 28th.

- Maria : To calculate age, you subtract the current year by the year of birth. But if you have not yet passed the anniversary date, you subtract 1.

- Peter : Ah, yeah. I forgot that!

``````function idade(ano_aniversario, mes_aniversario, dia_aniversario) {
var d = new Date,
ano_atual = d.getFullYear(),
mes_atual = d.getMonth() + 1,
dia_atual = d.getDate(),

ano_aniversario = +ano_aniversario,
mes_aniversario = +mes_aniversario,
dia_aniversario = +dia_aniversario,

quantos_anos = ano_atual - ano_aniversario;

if (mes_atual < mes_aniversario || mes_atual == mes_aniversario && dia_atual < dia_aniversario) {
quantos_anos--;
}

return quantos_anos < 0 ? 0 : quantos_anos;
}

``````

11.02.2014 / 07:24
13

I suggest comparing the month and day of the month: if today's date is greater than or equal to birth (i.e. birthday is over), then just make the difference between years. Otherwise, make a difference and subtract `1` :

``````function idade(nascimento, hoje) {
var diferencaAnos = hoje.getFullYear() - nascimento.getFullYear();
if ( new Date(hoje.getFullYear(), hoje.getMonth(), hoje.getDate()) <
new Date(hoje.getFullYear(), nascimento.getMonth(), nascimento.getDate()) )
diferencaAnos--;
return diferencaAnos;
}
``````

11.02.2014 / 07:28
7

A one-line solution, calculating from the number of days:

``````// calcula a idade considerando os parâmetros
// 'nascimento' e 'hoje' como objetos Date
return Math.floor(Math.ceil(Math.abs(nascimento.getTime() - hoje.getTime()) / (1000 * 3600 * 24)) / 365.25);
}
``````

The logic used is as follows:

• `Math.abs(nascimento.getTime() - hoje.getTime())` - returns the amount of milliseconds passed from `nascimento` to `hoje` . The `Math.abs` function returns the subtraction module, that is, it turns a negative number into a positive one and keeps the signal a positive one.
• `/ (1000 * 3600 * 24)` - calculates the number of days from the amount of milliseconds returned in the previous expression. Dividing by 1000, we have the amount of seconds; the number of seconds divided by 3600 we have the amount of hours (because in 1 hour 3600 seconds fit); and finally divide the amount of hours by 24, which will have the corresponding amount of days.
• `Math.ceil` - rounds up the decimal value of the previous operation, since it is considered that a day has passed even though the number of hours does not give 24 hours. Like for example a baby born last night, we consider that today morning has already 1 day of life.
• `/ 365.25` - finally, we calculate the year by dividing the total days by the total of days that fit in a year. The number 365.25 is because a year has approximately 365 days and 6 hours, which equals 365.25 days. The bixesto year comes from this difference, because every 4 years, the 6 hours disregarded in the calendar become 24 hours, that is another day.
• `Math.floor` - rounds down the amount of years. Well, it does not matter if the person celebrates his birthday tomorrow and he's 25,999 years old, he's 25 years old.
11.02.2014 / 13:17
5

How about calculating in a much more minimalist way? The calculation with ms is highly superior considering the performance. Ex: I was born on 12/17/1995. Then:

``````function calcAge(dateString) {
var birthday = +new Date(dateString);
return ~~((Date.now() - birthday) / (31557600000));
}

console.log(calcAge("Thu Dec 17 1995 00:51:54 GMT-0300 (BRT)"));``````
If code is run before 12/17/2015. I will still be 19 years old: P

The technique used can be found in: link And I thank Caio Ribeiro for showing me

05.11.2015 / 04:58
2

Hello, I've adapted the @Gabriel S. code to accept date in the Brazilian standard.

``````function calcIdade(data) {
var d = new Date,
ano_atual = d.getFullYear(),
mes_atual = d.getMonth() + 1,
dia_atual = d.getDate(),
split = data.split('/'),
novadata = split + "/" +split+"/"+split,
vAno = data_americana.getFullYear(),
vMes = data_americana.getMonth() + 1,
vDia = data_americana.getDate(),
ano_aniversario = +vAno,
mes_aniversario = +vMes,
dia_aniversario = +vDia,
quantos_anos = ano_atual - ano_aniversario;
if (mes_atual < mes_aniversario || mes_atual == mes_aniversario && dia_atual < dia_aniversario) {
quantos_anos--;
}
return quantos_anos < 0 ? 0 : quantos_anos;
}