# JavaScript How to Calculate Age from Birthdate

To calculate the age from birthdate in JavaScript, you can use this function:

```function age(birthdate) {
const today = new Date();
const age = today.getFullYear() - birthdate.getFullYear() -
(today.getMonth() < birthdate.getMonth() ||
(today.getMonth() === birthdate.getMonth() && today.getDate() < birthdate.getDate()));
return age;
}```

Here’s an example use of this function (called on 10th Feb 2023):

```const birthdate = new Date(2000, 2, 5);
const ageValue = age(birthdate);
console.log(ageValue);```

Output:

`22`

I hope this quick answer helps.

If you have trouble understanding how this code works and what exceptions there are in calculating age (leap years), please read along.

## Problems with Leap Years

To calculate a person’s age, you need to take into account the fact that a year has a slightly different length than a calendar year.

A year is approximately 365.25 days long, while a calendar year is 365 days.

To make up for this difference, every fourth year (except years that are divisible by 100 but not divisible by 400) is considered a leap year.

This discrepancy causes issues when trying to calculate a person’s age based on the number of days from their birthdate.

For example, a person born on January 1st, 2000 would turn 18 years old 6575 days later on January 1st, 2018 with 5 leap years between the time range.

However, a person born on January 1st, 2001 would turn 18 only 6574 days later on January 1st, 2019, with only 4 leap years in between.

All I’m trying to say is that if someone is 6574 days old, you cannot tell if they are 17 or 18 without knowing more information about their birthdate because of the leap years.

To calculate a person’s age in JavaScript, use the `Date` object and follow these two steps:

1. Subtract the birth year from the current year using the `getFullYear` method.
2. Subtract 1 if the current month and day come before the birth month and day using the `getMonth` and `getDate` methods.

Let’s take another look at the code you already saw in the introduction with some comments and refactorings to make it more clear to you.

```// Get today's date object
const today = new Date();

function age(birthdate) {
// A bool that represents if today's day/month precedes the birth day/month
const one_or_zero = (today.getMonth() < birthdate.getMonth()) ||
(today.getMonth() === birthdate.getMonth() &&
today.getDate() < birthdate.getDate());

// Calculate the difference in years from the date object's components
let year_difference = today.getFullYear() - birthdate.getFullYear();

// The difference in years is not enough.
// To get it right, subtract 1 or 0 based on if today precedes the
// birthdate's month/day.
// To do this, subtract the 'one_or_zero' boolean from 'year_difference'
// (This converts true to 1 and false to 0 under the hood.)
const age = year_difference - one_or_zero;

return age;
}

// Example age check:
console.log(age(new Date(2000, 0, 1)));
```

Output:

`23`

Note that in JavaScript, the months in a `Date` object are 0-based (i.e. January is 0, February is 1, etc.), whereas in Python they are 1-based (i.e. January is 1, February is 2, etc.).

A leapling is someone who was born on the leap day of a leap year (29th Feb).

This causes problems when calculating ages because, on a traditional year, that birthdate doesn’t exist. Usually, leaplings have their “official” birthday on 28th Feb or 1st Mar.

But technically, 1st Mar is the correct day because then a full year has passed since the leap day.

Let me show you how the above function’s logic handles the case where a person was born on February 29th, 2000 and we want to calculate their age on February 28th, 2001.

```let birthdate = new Date(2000, 1, 29); // 29th Feb 2000
let today = new Date(2001, 1, 28);     // 28th Feb 2001

let year_difference = today.getFullYear() - birthdate.getFullYear();  // 2001 - 2000 = 1

let one_or_zero = (today.getMonth() < birthdate.getMonth()) ||
(today.getMonth() === birthdate.getMonth() && today.getDate() < birthdate.getDate()) ? 1 : 0;

let age = year_difference - one_or_zero;

console.log(age);  // Output: 0
```

Since February 28th, 2001 is before the person’s birthdate of February 29th, 2000, the value `one_or_zero` is `1`. This means that their age would be calculated as `year_difference - 1`, which is `1 - 1 = 0`.

So the logic correctly identifies that a year hasn’t passed between Feb 29th, 2000 and Feb 28th, 2001.

## Wrap Up

In this tutorial, you learned how to calculate age in JavaScript using the built-in `Date` object.

Calculating age may seem straightforward, but it becomes more complex when because a year is not exactly 365 days long. Instead, it is approximately 365.25 days.

Scroll to Top