0

I have a registered some users in my app with their birth date. I'm trying to have a field called "age" that should show a number representing amount of years since born until today.

ie;

I have a user born 01-jan-2017 (today is 04-feb-2018)

the age output should be: 1 year old

and

I have a user born 05-feb-2017 (today is 04-feb-2018)

the age output should be: 0 year old

I have tried many thing... converting new Date() to time in milis and subtract new Date(birthdate) in milis. then making a new date with the dif and converting to years. but it doesn't return the correct amount of years always....

Finaly I have tried to implement the answer for this post Convert birthday to age in angularjs which have the exact same main question as I. I even see people commenting "thanks this works well" etc.... and it has 28 up votes. but I still don't get the correct value (years) always.... why!!??

Here is my implementation:

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'birthdate'
})
export class BirthdatePipe implements PipeTransform {

  transform(date: any, args?: any): any {
    const ageDifMs = Date.now() - new Date(date).getTime();
    const ageDate = new Date(ageDifMs);
    return Math.abs(ageDate.getUTCFullYear() - 1970);
  }

}

I tested it with following dates (remember today is: 04-feb-2018):

01-feb-2017 | birthdate = 0 should be 1
18-jan-2017 | birthdate = 1 should be 1
22-jan-2017 | birthdate = 1 should be 1
31-jan-2017 | birthdate = 0 should be 1

Is it really supposed to be difficult to calculate the years a person have lived based on their birth date without using some fancy framework? can that really be true? or am I missing something? Why are people in the other question having success with that code? is it a typescript/angular2 difference that is causing it to fail for me, since it seems to work in js/angular1....?

Rasmus Puls
  • 3,009
  • 7
  • 21
  • 58
  • Might be worth checking out `moment` – doublesharp Feb 04 '18 at 17:54
  • I would really like to not installing some plugin just to make óne calculation. But...... if it really turns out to be rocket science to calculate years from born -> now... then i guess i'll have to install it – Rasmus Puls Feb 04 '18 at 18:15
  • 1
    It was discussed in this post - https://stackoverflow.com/questions/4060004/calculate-age-given-the-birth-date-in-the-format-yyyymmdd . Problems with precision can appear from different factors - DST, leap years, time zones. The best approach is to rely on well tested lib. – mykhailo.romaniuk Feb 04 '18 at 18:45
  • 1
    Already covered indeed, the correct answer is https://stackoverflow.com/a/7091965/1347953 – Sébastien Feb 04 '18 at 20:20
  • Possible duplicate of [Calculate age given the birth date in the format YYYYMMDD](https://stackoverflow.com/questions/4060004/calculate-age-given-the-birth-date-in-the-format-yyyymmdd) – Sébastien Feb 04 '18 at 20:20
  • @Sébastien Thanks. neither showed up in possible answers or any google searches. But I still believe that the solutions are not 100% precise. So i will conclude that IF it need to be very precise then an external lib is necessary... – Rasmus Puls Feb 04 '18 at 21:48
  • 1
    I think there is a big difference between trying to find out exactly how much time in seconds has elapsed from a date of birth, or trying to find out a person's age, in years. In the seconds cas all you have to do is check if the date (feb 05) is passed and count the years. That is good enough and perfectly correct (answer is in provided link). The first question is harder to figure out because of leap seconds. – Sébastien Feb 04 '18 at 22:17

0 Answers0