SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking calculate the age

    hi, I've got a problem and i really need help.
    this is my code

    <script>
    var y= new Date();
    y.getFullYear() + '<br />';
    alert(y);
    var year = new Date();
    year.setFullYear(prompt('Enter the year','1990'),prompt('Enter the month','1'), prompt('Enter the day','1'));
    alert(year);
    var yy = y-year;
    alert(yy);
    </script>

    i want when a user write his/her birthday JS calculate the age. that's all

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by awesomest View Post
    i want when a user write his/her birthday JS calculate the age. that's all
    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.

    Code javascript:
    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.

    Code javascript:
    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;
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by awesomest View Post
    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.
    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?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •