0

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).

  • Welcome to SO and hope you have a good learning curve. What do you think the code flow here is? – Shreyas Jul 19 '20 at 03:30
  • Does this answer your question? [How can I calculate the number of years betwen two dates?](https://stackoverflow.com/questions/8152426/how-can-i-calculate-the-number-of-years-betwen-two-dates) – Tim Biegeleisen Jul 19 '20 at 03:32
  • Does this answer your question? [javascript - Age calculation](https://stackoverflow.com/questions/4076321/javascript-age-calculation) – Always Helping Jul 19 '20 at 03:34
  • Well in my understanding the parameters are just numbers and I want JS to treat the parameters as date. Since the default date format is yyyy-mm-dd, JS is taking the function parameters just like some other numbers. Since, I want the user to give his DOB as dd,mm,yyyy the date format should be changed. – Shreeyog Rajopadhye Jul 19 '20 at 03:36
  • @Shreyas, I think in the new Date() I should pass the parameters i.e. day,month,year rather than the function itself. Is that right? – Shreeyog Rajopadhye Jul 19 '20 at 03:43
  • @TimBiegeleisen Let me check this. – Shreeyog Rajopadhye Jul 19 '20 at 03:45

2 Answers2

2

First of all, welcome to Stack Overflow!

You were close to calculating the age. You were not passing the function parameters into the Date constructor and some other minor issues such as trying to use parseInt on a value that is already an integer:

let calculateAge = function(day, month, year) {
  var myBirthDate = new Date(year, month - 1, day),
    myBirthYear = myBirthDate.getFullYear(),
    myBirthMonth = myBirthDate.getMonth(),
    myBirthDay = myBirthDate.getDay();
  var currentDate = new Date(),
    currentYear = currentDate.getFullYear(),
    currentMonth = currentDate.getMonth(),
    currentDay = currentDate.getDay();
  var ageMonth = currentMonth - myBirthMonth;
  var ageDay = currentDay - myBirthDay;
  var age = currentYear - myBirthYear;
  if (ageMonth < 0 || (ageMonth == 0 && ageDay < 0)) {
    age = age - 1;
  }
  return age;
};
Robert Cooper
  • 2,160
  • 1
  • 9
  • 22
  • Hello Robert, Thank you for your answer and for correcting the code. One more question- is it necessary to have the date in yyyy-mm-dd format in Java Script? Coz here in India the convention is dd,mm,yyyy. So is there any way that I can change the format in Indian convention? – Shreeyog Rajopadhye Jul 19 '20 at 03:50
  • 1
    You need to pass in the arguments to the `Date` constructor as year, month, day (in that order). If you want to format the date, there are methods of doing that: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString – Robert Cooper Jul 19 '20 at 04:00
  • Since ECMAScript months are zero based, likely the OP needs `new Date(year, month - 1, day)`. – RobG Jul 19 '20 at 10:48
  • Adding to the same code, instead of passing the values in the function call, if I asked the user to give his/her birthdate and then passed the prompted value in the function call the output is NaN. Why the output is not a number? Since I asked the user to give his birthday, then he would anyway give the day, month and year. – Shreeyog Rajopadhye Jul 20 '20 at 06:18
  • @ShreeyogRajopadhye You need to make sure to call `calculateAge` with numbers and not strings. Make sure that when you receive values from the user, that you convert any strings to numbers. – Robert Cooper Jul 20 '20 at 06:20
0

Using the experimental Temporal proposal:

const calculateAge = (day, month, year) => {
  const birthday = Temporal.DateTime.from({ day, month, year });
  const today = Temporal.DateTime.from(Temporal.now.date());
  return today.difference(birthday, { largestUnit: 'years' }).years;
}

calculateAge(24, 4, 1993); // 27
GirkovArpa
  • 4,427
  • 4
  • 14
  • 43