JSFiddle - React, Tailwind, and code Playground

by Tanveer Khan

JavaScript

// writing a function to get the actual age of the person
function getAge(dateString) {
	// getting the current date
  var today = new Date();
  // getting the users actual birthday
  var birthDate = new Date(dateString);
  // substracting actual birthday year from current year and storing it in a variable called age
  var age = today.getFullYear() - birthDate.getFullYear();
  // substracting birth month from current month
  var m = today.getMonth() - birthDate.getMonth();
  // calculating the age
  if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
    age--;
  }
  // storing the value in age
  return age;
}
// logging the date in years in the console log
console.log('age: ' + getAge("1981/11/24"));