0

How can I calculate Birthdate from given age using javascript.

example:

I am entering my age as 23 years 1 month 26 days.

then I need to get birthdate as 1995-05-30.

how this can be done.

priya_singh
  • 2,478
  • 1
  • 14
  • 32
ACoder
  • 51
  • 1
  • 7
  • Possible duplicate of [javascript - Age calculation](https://stackoverflow.com/questions/4076321/javascript-age-calculation) – Jorge Fuentes González Jul 26 '17 at 06:23
  • 1
    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) – Durga Jul 26 '17 at 06:24
  • You could try convert it to seconds, awell as the current date time, then substract and the creating a new date from it – Bas Pauw Jul 26 '17 at 06:28

1 Answers1

2

If you are working with dates, I recommend using moment library:

https://momentjs.com/

Using moment you can easily subtract intervals from dates. For example:

moment().subtract(10, 'days')

In your case:

var bDay = moment().subtract(23, 'years');
bDay.subtract(1, 'months');
bDay.subtract(26, 'days');
Zsolt Tolvaly
  • 3,528
  • 4
  • 26
  • 25