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.
Page 2 of 2 FirstFirst 12
Results 16 to 24 of 24
  1. #16
    Regular Coder
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I took the original code and changed it a little and this works perfect. The only problem is like PhotoJoe said, is that say you were born in 1984 then any month or day you use for this year will return 21 even if you haven't had your b-day yet. You need to add a couple lines that pretty much checks to see if the month/day they entered is before or on this day and if it isn't then subtract 1.

    Code:
    <script type="text/javascript">
    <!--
    function calcAge() {
    
    var dob_month;
    var dob_day;
    var dob_year;
    var dob;
    
    dob = document.form1.elements.dob.value;
    
    var birthDay = dob.split("-");
    
    dob_month = birthDay[0];
    dob_day = birthDay[1];
    dob_year = birthDay[2];
    
    var time=new Date();
    var lmonth=time.getMonth() + 1;
    var date=time.getDate();
    var year=time.getFullYear();
    
    var returnAge = year - dob_year;
    
    document.form1.r_age.value=returnAge
    
    alert("Your age is: " + returnAge);
    
    }
    //-->
    </script>
    
    
    <body>
    
    <form name="form1">
    
    <p>Enter date of birth in format MM-DD-YYYY
    <input id="dob" type="text" name="dob" value="">
    </p>
    
    <p id="itext">
    Click the button below to calculate your age
    </p>
    
    <p>
    <input type="button" value="Calculate" onclick="calcAge()">
    </p>
    
    <p>
    Your age is: <input type="text" name="r_age" size="10">
    </p>
    </form>
    I'm only telling you how I would do it, not how its supposed to be done. ;)

  2. #17
    Regular Coder
    Join Date
    Feb 2005
    Location
    Atlanta, GA
    Posts
    171
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I did not have any considerations of month and day before, but always intended to put that in. Here is the "test code". I have it also in an actual page that I am putting together at http://www.tdavisconsulting.com/kenerly. The test code works, but the actual page does not. I am not sure where to put the calcAge(). I have it on the DOB along with the date selector process (with onFocus). It works, but the date does not get put into the AGE field until the 2nd time.

    Maybe I should use onBlur?

    <script type="text/javascript">
    function calcAge(returnAge) {

    var dob_month;
    var dob_day;
    var dob_year;
    var dob;
    var message;

    dob = document.form1.elements.dob.value;

    var birthDay = dob.split("-");

    dob_month = birthDay[0];
    dob_day = birthDay[1];
    dob_year = birthDay[2];

    var time=new Date();
    var lmonth=time.getMonth() + 1;
    var date=time.getDate();
    var year=time.getYear() + 1900;

    returnAge = year - dob_year;

    if (dob_month > lmonth) {
    returnAge--;
    }

    if (dob_month == lmonth && dob_day > date) {
    returnAge--;
    }

    document.form1.r_age.value=returnAge
    return returnAge;

    }
    </script>


    <body>

    <form name="form1">

    <p>Enter date of birth in format MM-DD-YYYY
    <input id="dob" type="text" name="dob" value="">
    </p>

    <p id="itext">
    Click the button below to calculate your age
    </p>

    <p>
    <input type=button value='Click here to calculate your age' onclick="calcAge()">
    </p>

    <p>
    Your age is: <input type="text" name="r_age" size="10" readonly style="border:0px solid black; background-color:transparent;">
    </p>

    </form>

  3. #18
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    use getFullYear() instead of getYear()+1900
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  4. #19
    Regular Coder
    Join Date
    Feb 2005
    Location
    Atlanta, GA
    Posts
    171
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Thanks. I will change to using getFullYear(), but I dont think that will resolve my most pressing issue right now. I have my test code working, but the actual form has a problem.

    I dont know where to put the calcAge() function call.
    Does anyone have any suggestions?

    Thanks!

  5. #20
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Here it is:
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <
    html>
    <
    head>
    <
    title>Untitled Document</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <
    meta http-equiv="Content-Style-Type" content="text/css">
    <
    meta http-equiv="Content-Script-Type" content="text/javascript">
    <
    script type="text/javascript">
    var 
    today = new Date();
    var 
    ty today.getFullYear();
    var 
    tm today.getMonth();
    var 
    td =today.getDate();
    function 
    calcAge(){
    var 
    inp document.getElementById('dob').value.split('-');
    var 
    dif ty-Number(inp[2]);
    dif=((tm<inp[0])||(tm==Number(inp[0])&&td<Number(inp[1])))?dif-1:dif;
    document.getElementById('ag').firstChild.data=dif;
    }
    </script>
    </head>
    <body>
    <p>Enter date of birth in format MM-DD-YYYY
    <input id="dob" type="text" name="dob" value="">
    </p>

    <p id="itext">
    Click the button below to calculate your age
    </p>

    <p>
    <input type=submit onclick="calcAge()">
    </p>

    <p>
    Your age is:<span id="ag">&nbsp;</span>

    </body>
    </html> 
    Last edited by Kor; 11-24-2005 at 06:02 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  6. #21
    Regular Coder
    Join Date
    Oct 2005
    Location
    Arizona
    Posts
    336
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Kor,

    It seems a lot of people are working the date object these days.

    I still have a hard time understanding the conditional statement. So once again would you mind explainging this line of code?

    dif=((tm<inp[0])||(tm==Number(inp[0])&&td<Number(inp[1])))?dif-1:dif;

    I have not seen the Number() function before, what is it's syntax?
    Last edited by PhotoJoe47; 11-24-2005 at 08:50 PM.

  7. #22
    Regular Coder
    Join Date
    Oct 2005
    Location
    Arizona
    Posts
    336
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok,
    I when back and redid the line of code like this. Now it makes a little more sense.
    dif= ((tm<inp[0]) || (tm == Number(inp[0])&& td<Number(inp[1])))? dif-1 : dif;

    Lets see if I can explain it and you let me know if I'm right.

    dif is the number of years between the two date objects. The conditional statement will check if the value of dif needs to be change by -1 or not.
    It will subtract 1 if today's month is less than the input month.
    Or it will subtract 1 if today's month is equal to the input month and today's day of the month is less than the input's day of the month.

    I'm guessing that the Number() is just making sure that a number is being compared?

    How did I do?

    Well it is time to go eat some turkey.

    Happy Thanksgiving to all.

  8. #23
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    The basic JavaScript types each have a global object like that.

    It's a way to convert other objects to numbers. Here's a table you can see how it behaves in:
    Code:
    n           Number(n)   parseInt(n) parseInt(n,10)  parseFloat(n)   isNaN(n)    eval(n)
    ----        ----        ----        ----            ----            ----        ----
    ''          0           NaN         NaN             NaN             false       undefined
    '8'         8           8           8               8               false       8
    '.8'        0.8         NaN         NaN             0.8             false       0.8
    '-8'        -8          -8          -8              -8              false       -8
    '08'        8           0           8               8               false       8
    '1e3'       1000        1           1               1000            false       1000
    '1d3'       NaN         1           1               1               true        ERROR
    '0x1d3'     467         467         0               0               false       467
    'd'         NaN         NaN         NaN             NaN             true        ERROR
    Number(n) behaves exactly the same as the numeric operator shortcuts we sometimes use: +n, 1*n, n-0, n/1
    Last edited by liorean; 02-02-2006 at 06:29 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  9. #24
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    dif is the number of years between the two date objects. The conditional statement will check if the value of dif needs to be change by -1 or not.
    It will subtract 1 if today's month is less than the input month.
    Or it will subtract 1 if today's month is equal to the input month and today's day of the month is less than the input's day of the month.
    Exactly. About the Number() liorean had a comrehensive answer above.
    It seems a lot of people are working the date object these days.
    There must be a Date festival or contest somewere...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


 
Page 2 of 2 FirstFirst 12

Posting Permissions

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