JavaScript

How to Calculate Age Using the Given Birth Date in the Format YYYYMMDD

Sometimes, there is a requirement in the forms to enter the data in a particular format, such as the date of birth. The birth date is entered in multiple formats, like YYYYMMDD, YYYY-MM-DD, DD-MM-YYYY, and so on. More specifically, calculating age using a birth date in a specific format is a critical task.

This write-up will illustrate the method for calculating age using the birth date in YYYYMMDD format.

How to Calculate Age Using the Given Birth Date in the Format YYYYMMDD?

To calculate age, use the predefined methods of the “Date” object, such as the “Date() constructor”, “getFullYear()”, “getMonth()”, and “getDate()” method, with the “substring()” method. The “substring()” method will split the given string into substrings by passing the start and the end indexes of the string.

Example
In a JavaScript file, use the below-given code for calculating age in the particular format:

function calculateAge(dob) {
 const today = new Date();
 const getBirthYear = parseInt(dob.substring(0, 4));
 const getBirthMonth = parseInt(dob.substring(4, 6));
 const getBirthDay = parseInt(dob.substring(6));
 var calAge = today.getFullYear() - getBirthYear;
 const calAgeMonth = today.getMonth() + 1 - getBirthMonth;
 const calAgeDay = today.getDate() - getBirthDay;
 if (calAgeMonth < 0 || (calAgeMonth === 0 && calAgeDay < 0)) {
  calAge = calAge - 1;
 }
 return calAge;
}

In the above following code:

  • First, define a function named “calculateAge()” that takes the date of birth in the specified format. Call the “Date()” constructor that returns the current date and stores it in a variable “today”.
  • Divide the given date into three parts, such as “getBirthYear”, “getBirthMonth” and “getBirthDay” using the “substring()” method that takes the start and the end indexes of the string.
  • Now, calculate the age by taking the difference between the current year with the birth year, a current month with the birth month, and the current date with the birth date.
  • Check the condition if the given date of birth is in the future, then subtract one from the difference between the current and the birth years.
  • Finally, return the calculated age to the function.

Now, for calculating the age of the “user1”, call the “calculateAge()” function, by passing the date of birth in the specified format:

const u1Age = calculateAge("19980114");
console.log("Age of user1 is: " + u1Age);

Call the function again for calculating the age of the second user:

const u2Age = calculateAge("19940516");
console.log("Age of user2 is: " + u2Age);

Output

The above output indicates that age has been successfully calculated using the given birth date in the YYYYMMDD format.

Conclusion

For calculating age using the given birth date in a specific format, use the predefined methods of the “Date” object, such as the “Date() constructor”, “getFullYear()”, “getMonth()”, “getDate()” method, with the “substring()” method. This write-up illustrated the method for calculating age using the birth date in YYYYMMDD format.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.