I am writing a program in JS to find age. The parameters to a function calculateAge ()
are day, month, year (DOB of a person) and output should be the age of the person.
I tried the following code
let calculateAge = function(day, month, year){
var myBirthDate = new Date(calculateAge),
myBirthYear = myBirthDate.getFullYear(),
myBirthMonth = myBirthDate.getMonth(),
myBirthDay = myBirthDate.getDay()
let currentDate = new Date(),
currentYear = currentDate.getFullYear(),
currentMonth = currentDate.getMonth(),
currentDay= currentDate.getDay()
var age = currentYear - myBirthYear
var ageMonth = currentMonth - myBirthMonth
var ageDay = currentDay-myBirthDay
if (ageMonth<0 || (ageMonth == 0 && ageDay<0)){
age = parseInt(age)-1
}
}; alert (calculateAge(24,04,1993))
The output is undefined instead of 27. My understanding is, JS does not know if the input parameters are date values. So I would like to know if there's a way to tell JS that the parameters are actually the date values (day,month,year format).