awesomest — 2012-02-25T00:56:48-05:00 — #1
hi, I've got a problem and i really need help.
this is my code
var y= new Date();
y.getFullYear() + '<br />';
var year = new Date();
year.setFullYear(prompt('Enter the year','1990'),prompt('Enter the month','1'), prompt('Enter the day','1'));
var yy = y-year;
i want when a user write his/her birthday JS calculate the age. that's all
paul_wilkins — 2012-02-25T06:44:56-05:00 — #2
Be warned - the month value that the Date object uses is between 0 and 11
Having said that, given that the year month and day are numbers in a manner that we are used to using, a script can remove one from the month and work out the persons age in the following manner.
Find out if the person has had they birthday yet. If they have, they will be todays year minus their birth year old.
You can find out if they have had their birthday by comparing this year with their birth month and day, to the
If they are yet to have their birthday, they will be a year younger than that.
var isBeforeBirthday = new Date(new Date().getYear(), month - 1, day) < new Date(),
age = new Date().getFullYear() - (isBeforeBirthday ? 1 : 0) - year;
And if we want to get picky, we can ensure that even if todays day is their birthday, that we compare midnight of today to their birthdate.
var midnightToday = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0),
isBeforeBirthday = new Date(new Date().getYear(), month - 1, day) < midnightToday,
age = midnightToday.getFullYear() - (isBeforeBirthday ? 1 : 0) - year;
awesomest — 2012-02-26T11:42:34-05:00 — #3
i think you didn't get me. i want that the user put the birth date. . with prompt or input in HTML
but thanks for the explanation.
paul_wilkins — 2012-02-26T11:53:51-05:00 — #4
I think you didn't get me. From the year month and date of a person's birthday, I have created a clear way in which to turn their birthday in to their age.
Have I misunderstood your intentions? Do you require someone to create completely working code for you too, to add on everything else that is needed for it to be a fully working example?
How can we help you further in this?