Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Not as easy as that, as need to check entered date is valid (not 31st February) and whether or not birthday has been reached this year. You cannot simply subtract the birth year from the current year.

    Code:
    Enter Date Of Birth in YYYY-MM-DD format <input type = "text" name = "dob" id = "dob" size = "10" maxlength = "10" onblur = "checkAge()">
    
    <script type="text/javascript"> 
    
    function checkAge(){ 
    var today = new Date(); 
    var d = document.getElementById("dob").value;
    if (!/\d{4}\-\d{2}\-\d{2}/.test(d)) {   // check valid format
    showMessage();
    return false;
    }
    
    d = d.split("-");
    var byr = parseInt(d[0]); 
    var nowyear = today.getFullYear();
    if (byr >= nowyear || byr < 1900) {  // check valid year
    showMessage();
    return false;
    }
    var bmth = parseInt(d[1],10)-1;   // radix 10!
    if (bmth <0 || bmth >11) {  // check valid month 0-11
    showMessage() 
    return false;
    }
    var bdy = parseInt(d[2],10);   // radix 10!
    var dim = daysInMonth(bmth+1,byr);
    if (bdy <1 || bdy > dim) {  // check valid date according to month
    showMessage();
    return false;
    }
    
    var age = nowyear - byr;
    var nowmonth = today.getMonth();
    var nowday = today.getDate();
    if (bmth > nowmonth) {age = age - 1}  // next birthday not yet reached
    else if (bmth == nowmonth && nowday < bdy) {age = age - 1}
    
    alert('You are ' + age + ' years old'); 
    if (age <= 15) {
    alert ("You are 15 years old or less!");  
    }
    }
    
    function showMessage() {
    if (document.getElementById("dob").value != "") {
    alert ("Invalid date format or impossible year/month/day of birth - please re-enter as YYYY-MM-DD");
    document.getElementById("dob").value = "";
    document.getElementById("dob").focus();
    }
    }
    
    function daysInMonth(month,year) {  // months are 1-12
    var dd = new Date(year, month, 0);
    return dd.getDate();
    } 
    
    </script>
    As this is probably homework here is a simpler script which performs minimal error checking (Garbage In, Garbage Out!):-

    Code:
    <script type = "text/javascript">
    
    var today = new Date();
    var nowyr = today.getFullYear();
    var dob = prompt("Enter your age in YYYY/MM/DD format","")
    var dobsplit = dob.split("/");
    var byr = dobsplit[0];
    var bmth = dobsplit[1];
    var bday = dobsplit[2];
    var age = nowyr-byr;
    var d = new Date(nowyr,bmth-1,bday);  // months in Javascript are 0-11
    if (d > today) {  // birthday not yet reached
    age --;  so subtract 1
    }
    
    if ((isNaN(age)) || (age < 0)){
    alert ("Invalid data!");
    }
    
    else {alert ("Your age is " + age);
    }
    
    </script>
    Quizmaster: In which century was Coca Cola invented?
    Contestant: The eighteenth
    Last edited by Philip M; 02-25-2012 at 10:03 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Regular Coder
    Join Date
    Sep 2010
    Location
    Far far away
    Posts
    122
    Thanks
    0
    Thanked 16 Times in 16 Posts
    Code:
    var birthdate = prompt("Enter your age in YYYY/MM/DD format","");
    
    var b = new Date(birthdate);
    if ( isNaN(b) ) {
    
        alert('Invalid age format');
    
    } else {
    
        var now = new Date();
    
        var age = now.getFullYear() - b.getFullYear();
        if ( now.getMonth() < b.getMonth() ) {
            age--;
        }
    
        alert('Age is ' + age);
    
    }

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    siberia-man - you must take account of the birth day, not just the birth month!

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Regular Coder
    Join Date
    Sep 2010
    Location
    Far far away
    Posts
    122
    Thanks
    0
    Thanked 16 Times in 16 Posts
    Philip M, yes! Your are totally right. I forgot this.

    So. To allow the homeworker to stress his brain by himself to find which line he should fix I just show the part of the corrected line here :-)
    Code:
    now.getMonth() - b.getMonth() < 0 || now.getDate() - b.getDate() < 0

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by siberia-man View Post
    Philip M, yes! Your are totally right. I forgot this.

    So. To allow the homeworker to stress his brain by himself to find which line he should fix I just show the part of the corrected line here :-)
    Code:
    now.getMonth() - b.getMonth() < 0 || now.getDate() - b.getDate() < 0
    Sorry, still wrong! Your brain has frozen! Try entering 2000/3/3 and you get age 11, not 12.

    Code:
    <script type = "text/javascript">
    
    var birthdate = prompt ("Enter your age in YYYY/MM/DD format","");
    
    var bdate = new Date(birthdate);
    if ( isNaN(bdate) ) {
    alert ('Invalid age format');
    } 
    else {
    var now = new Date();
    var age = now.getFullYear() - bdate.getFullYear();
    var mth = now.getMonth();
    var bmth = bdate.getMonth();
    if (mth - bmth <0  ) {age--}
    else if ((mth-bmth== 0) && (now.getDate() - bdate.getDate() < 0)) {age--}
    alert('Age is ' + age);
    
    }
    </script>
    Last edited by Philip M; 02-25-2012 at 05:10 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    awesomest (02-26-2012)

  • #7
    Regular Coder
    Join Date
    Sep 2010
    Location
    Far far away
    Posts
    122
    Thanks
    0
    Thanked 16 Times in 16 Posts
    Quote Originally Posted by Philip M View Post
    ... Try entering 2000/3/3 and you get age 11, not 12. ...
    It is very strange and it has interested me how did you got this result. I especially have tested this in several environments such as Firefox, MSIE, Iron (Chromium), WSH, Rhino, NodeJS. All of them have shown the result 11.

    I just want to say that age 11 in this case is correct value.
    Last edited by siberia-man; 02-26-2012 at 12:21 AM.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by siberia-man View Post
    It is very strange and it has interested me how did you got this result. I especially have tested this in several environments such as Firefox, MSIE, Iron (Chromium), WSH, Rhino, NodeJS. All of them have shown the result 11.

    I just want to say that age 11 in this case is correct value.
    Whoops! My bad!
    Last edited by Philip M; 02-26-2012 at 09:04 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #9
    Regular Coder
    Join Date
    Sep 2010
    Location
    Far far away
    Posts
    122
    Thanks
    0
    Thanked 16 Times in 16 Posts
    After your words I thught that I met that case when engine tries to execute the whole condition before making the next step. But I remember JS is "lazy" and does not execute the rest of the condition if the result is known. So I began to test in all ebvironments available for me.

  • #10
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Sorry, still wrong! Your brain has frozen! Try entering 2000/3/3 and you get age 11, not 12.

    Code:
    <script type = "text/javascript">
    
    var birthdate = prompt ("Enter your age in YYYY/MM/DD format","");
    
    var bdate = new Date(birthdate);
    if ( isNaN(bdate) ) {
    alert ('Invalid age format');
    } 
    else {
    var now = new Date();
    var age = now.getFullYear() - bdate.getFullYear();
    var mth = now.getMonth();
    var bmth = bdate.getMonth();
    if (mth - bmth <0  ) {age--}
    else if ((mth-bmth== 0) && (now.getDate() - bdate.getDate() < 0)) {age--}
    alert('Age is ' + age);
    
    }
    </script>
    thanks guys! this code is what i wanted.
    thank you again
    Last edited by awesomest; 02-26-2012 at 05:19 PM.

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    I notice that in all the scripts
    prompt ("Enter your age in YYYY/MM/DD format","");
    should of course read
    prompt ("Enter your birthdate in YYYY/MM/DD format","");

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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