Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get age from Birthdate [duplicate]

Tags:

Possible Duplicate:
Calculate age in JavaScript

In some point of my JS code I have jquery date object which is person's birth date. I want to calculate person's age based on his birth date.

Can anyone give example code on how to achieve this.

like image 692
Aram Gevorgyan Avatar asked Apr 04 '12 09:04

Aram Gevorgyan


People also ask

How do I calculate age in Excel from date of birth?

Simply by subtracting the birth date from the current date. This conventional age formula can also be used in Excel. The first part of the formula (TODAY()-B2) returns the difference between the current date and date of birth is days, and then you divide that number by 365 to get the numbers of years.

How do I calculate age in Excel without Datedif?

USING YEARFRAC FUNCTION: YEARFRAC function in Excel returns a decimal value that represents fractional years between two dates. We can use this function to calculate age.

How do I calculate age from YYYY in Excel?

=DATEDIF(B2,TODAY(),"Y") (B2,TODAY(),"Y") tells DATEDIF to calculate the difference between the date in cell B2 (the first birthday listed) and the current date ( TODAY() ). It outputs the calculation in years ( "Y" ). If you'd rather see the age in days or months, use "D" or "M" instead.


2 Answers

Try this function...

function calculate_age(birth_month,birth_day,birth_year) {     today_date = new Date();     today_year = today_date.getFullYear();     today_month = today_date.getMonth();     today_day = today_date.getDate();     age = today_year - birth_year;      if ( today_month < (birth_month - 1))     {         age--;     }     if (((birth_month - 1) == today_month) && (today_day < birth_day))     {         age--;     }     return age; } 

OR

function getAge(dateString)  {     var today = new Date();     var birthDate = new Date(dateString);     var age = today.getFullYear() - birthDate.getFullYear();     var m = today.getMonth() - birthDate.getMonth();     if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate()))      {         age--;     }     return age; } 

[See Demo.][1] [1]: http://jsfiddle.net/mkginfo/LXEHp/7/

like image 173
Ashwini Agarwal Avatar answered Sep 22 '22 17:09

Ashwini Agarwal


JsFiddle

You can calculate with Dates.

var birthdate = new Date("1990/1/1"); var cur = new Date(); var diff = cur-birthdate; // This is the difference in milliseconds var age = Math.floor(diff/31557600000); // Divide by 1000*60*60*24*365.25 
like image 30
Frank van Wijk Avatar answered Sep 21 '22 17:09

Frank van Wijk