Incorrectly considers birthday. t, e if today is February 26, 2019, and others are somewhere on 10.10.2019., then it shows 25 years.

if(userInfo && userInfo.birthDay){ let date = new Date(HexToSignedInt(userInfo.birthDay)); let optionsTime = { year: 'numeric', month: '2-digit', day: '2-digit' }; birthDay = date.toLocaleString('en-GB', optionsTime).replace(',', ' '); // years = this.calculateAge(date.getFullYear(), date.getUTCMonth() + 1, date.getUTCDate()); years = this.calculateAge(date.getFullYear(), date.getMonth(), date.getDate()); } calculateAge(year, month, day) { let currentDate = new Date(); let currentYear = currentDate.getFullYear(); let currentMonth = currentDate.getMonth(); let currentDay = currentDate.getDate(); // You need to treat the cases where the year, month or day hasn't arrived yet. let age = currentYear - year; // if (currentMonth < month) { if (currentDate < month) { return age; } else { if (currentDay >= day) { return age; } else { age--; return age; } } } 

  • What is this if (currentDate <month)? - Enikeyschik
  • then it shows 25 years - and how much should it be? - Enikeyschik
  • Well 24. The birthday is 10.10.1994, and now is 02.26.2019. And currentDate forgot to comment out, there was currentMonth - Oleksey
  • Well, excuse me, I do not know when your birthday is there ... Judging by the question, the person has not yet been born. - Enikeyschik February
  • there was a mistake, I’m saying 10.10.1994 is worth it, in short, it is plus a month, if you set it up a month in advance, like 10.03.1994. he thinks right, but if more than 1 month, he thinks wrong - Oleksey

2 answers 2

The comparison logic should be simple:

  1. If the current month is less than a month in the date of birth, the age is equal to the difference of years -1
  2. otherwise,
    1. if the current day is less than the day in the date of birth - the age is equal to the difference of years -1
    2. otherwise the difference of the years

Example in numbers:

Date of birth: 2000-02-10

  1. Today is 2019-02-09, i.e. one day before:

    1. Year difference 2019-2000 = 19
    2. current month matches
    3. the current day is less than the day of the date of birth, the result is 19-1 = 18.
  2. Today 2019-02-10, i.e. birthday today

    1. Year difference 2019-2000 = 19
    2. current month matches
    3. the current day is not less than the day of the date of birth, the result is 19.
  3. Today 2019-02-11, i.e. day after

    1. Year difference 2019-2000 = 19
    2. current month matches
    3. the current day is not less than the day of the date of birth, the result is 19.
  4. Today is 2019-01-09, i.e. a month earlier

    1. Year difference 2019-2000 = 19
    2. the current month is less - the result is 19-1 = 18

Example:

 function calculateAge(year, month, day) { let currentDate = new Date(); let currentYear = currentDate.getFullYear(); let currentMonth = currentDate.getMonth(); let currentDay = currentDate.getDate(); console.log(currentYear, currentMonth, currentDay); console.log(year, month, day); // You need to treat the cases where the year, month or day hasn't arrived yet. let age = currentYear - year; if (currentMonth < month) { return age - 1; } return currentDay < day ? age - 1 : age; } console.log(calculateAge(2000, 1, 10)); console.log(calculateAge(2000, 1, 26)); console.log(calculateAge(2000, 1, 27)); console.log(calculateAge(2000, 2, 10)); 

  • (2000, 1, 10) - what's the date? - Enikeyschik
  • @ Enikeyschik, February 10, 2000. (counting down months from 0) - Grundy
  • @Grundy code gives incorrect result, did I replace currentDay <day? age - 1: age; and it became the norm, thank you. - Oleksey
  • @Oleksey, corrected the code, the problem was with the months too - Grundy
  • @Oleksey added more examples - Grundy

@ Oleksey why so difficult?

 const currentDate = new Date() const birthDate = new Date('1994-10-10'); const MILLISECONDS_IN_YEAR = 31536000000; const age = Math.floor((currentDate - birthDate ) / MILLISECONDS_IN_YEAR); console.log(`Возраст: ${age}`); Не учел високосные годы =( 

or using momentJS

 const age = moment().diff(moment('1994-10-10'), 'year') 
  • And to determine the age in years in milliseconds is, of course, no difficulties: D Not to mention that this code in some cases gives the wrong result. - Enikeyschik
  • Maybe this is certainly easier, but it gives the wrong result. take today (02.26.2019) let's say DR 02.24.2014 gives 25 years, as well as 02.02.2014. Those. DR passes, and the age does not change. And the problem is that there are leap years, and you have laid the constant for 365 days. (And if you suddenly decide how to take into account leap years, that is, ru.wikipedia.org/wiki/… ) - Mike
  • Yes, I forgot about leap years - Artem Vasilyev
  • not my code, they gave me a fix, but myself for an internship. - Oleksey