calculate the age

Discussion in 'JavaScript' started by awesomest, Feb 24, 2012.

  1. #1
    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
     
    awesomest, Feb 24, 2012 IP
  2. awesomest

    awesomest Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    no one? really????
     
    awesomest, Feb 26, 2012 IP
  3. rainborick

    rainborick Well-Known Member

    Messages:
    424
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    120
    #3
    There's bound to be dozens of example scripts for this on the web. Try searching on "javascript age calculator" and I'm sure you'll find one.
     
    rainborick, Feb 26, 2012 IP
  4. hdewantara

    hdewantara Well-Known Member

    Messages:
    540
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #4
    Those y, year and yy are date objects.
    You need to convert them to numbers - via .getFullYear() - to be able to perform some math.
    
    var y= new Date();
    var year = new Date();
    year.setFullYear(
        prompt('Enter the year',1990),
        prompt('Enter the month',1), 
        prompt('Enter the day',1));
    var yy = y.getFullYear() - year.getFullYear();
    alert(yy);
    
    Code (markup):
     
    hdewantara, Feb 27, 2012 IP
  5. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #5
    JohnnySchultz, Feb 29, 2012 IP