-4

I have birthday in following format:

1982-09-20

I need to get the exact age of the person from that birthdate ( compared with the current date).

What's the easiest way to do this in JS? Can someone help me out please?

Thanks!

perkes456
  • 1,163
  • 4
  • 25
  • 49

1 Answers1

1

I assume you want the exact age in years. If you want another value, it's all in that magic number of have in the function below, a bit verbose for clarity:

var MILLISECONDS_IN_A_YEAR = 1000*60*60*24*365;
function get_age(time){
    var date_array = time.split('-')
    var years_elapsed = (new Date() - new Date(date_array[0],date_array[1],date_array[2]))/(MILLISECONDS_IN_A_YEAR);
    return years_elapsed; }

You would call: get_age ('1982-09-20')

This is probably going to be marked as a duplicate and deleted. There might be a better in built way, but this should work fine for your needs.

Bradford Medeiros
  • 1,581
  • 2
  • 9
  • 6