179
rated 0 times
[
183]
[
4]
/ answers: 1 / hits: 57421
/ 12 Years ago, mon, september 3, 2012, 12:00:00
I want do show from user date birthday that a few days and months and years last.
Here is my code, as taken from here: Calculate age in JavaScript
How can it continue with the month and day, as:
user birthday is : 2010/04/29
The result should be like this: 2 years, 4 months, 5 days old.
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;
}
alert(getAge('2010/04/29'));
DEMO: http://jsfiddle.net/jFxb5/
More From » jquery