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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Using pull downs and calc birthdate

    Hello,

    I am trying to use select options to get birthdate and calculate the age. I got the birthday with "birthDay = document.form1.selDate;" However i'm lost on the birthMonth and birthYear. I've looked at lots of examples. The birthMonth probably needs to be converted to integer, or use 1 thru 12 rather than Jan thru Dec. But birthYear i'd think would come back as a valid number. Is anyone familiar with this?

    Thanks.

    Code:
    <html>
    <head>
    <title>Main</title>
    <script language="javascript">
    <!--
    function Age()
    {
        var birthDay;
        var birthMonth;
        var birthYear;
        var userAge;
        var currentDay = new Date();
        var dayNow = currentDay.getDate();
        var monthNow = (currentDay.getMonth());
        var yearNow = (currentDay.getFullYear());
    
        birthDay = document.form1.selDate;
        
        // Don't know how to get birthmonth????
    //  birthMonth = (parseInt(document.form1.selMonth.value)-1);
        
        birthYear = document.form1.selYear;
        // alert("birthyear " + birthYear); ( gives "birthYear [object HTMLSelectElement]")
    
        if((monthNow > birthMonth)||(monthNow == birthMonth & dayNow >= birthDay))
        { userAge = birthYear; }
        else {    userAge=birthYear+1;}
        alert("As of today, " + currentDay +' \n'+", you are: " + (yearNow - userAge) + " years old");
    }
    //-->
    </script>
    </head>
    <body>
    <form name="form1" id="startOfForm">
    <table><tbody>
        <tr>
            <td class="question"><label for="birthdate">Enter Birthdate</label></td>
            <td>
                <select name=selMonth>
                <option value=0 selected>Month</option>    <option selected value="Jan">Jan</option>
                <option selected value="Feb">Feb</option> <option selected value="Mar">Mar</option>
                <option selected value="Apr">Apr</option> <option selected value="May">May</option>
                <option selected value="Jun">Jun</option> <option selected value="Jul">Jul</option>
                <option selected value="Aug">Aug</option> <option selected value="Sept">Sept</option>
                <option selected value="Oct">Oct</option> <option selected value="Nov">Nov</option>
                <option selected value="Dec">Dec</option>
                </select>
                <select name=selDate>
                <option value=0 selected>Date</option> <option selected value="1">1</option>
                <option selected value="2">2</option> <option selected value="3">3</option>
                <option selected value="4">4</option> <option selected value="5">5</option>
                <option selected value="6">6</option> <option selected value="7">7</option>
                <option selected value="8">8</option> <option selected value="9">9</option>
                </select>
                <select name=selYear>
                <option value=0 selected>Year</option> <option value="2001">2001</option>
                <option value="2000">2000</option> <option value="1999">1999</option>
                <option value="1998">1998</option> <option value="1997">1997</option>
                <option value="1996">1996</option> <option value="1995">1995</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>
            <input name="cmd" type=button value="Age"
                    onclick="Age()" style="width:150px; height:30px;">
            </td>
        </tr>
    </tbody></table>
    </form>
    </body>
    </html>

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,342
    Thanks
    13
    Thanked 349 Times in 345 Posts
    as a Rule of Thumb, anything you get from a form control is a string, so you have to take care if you use the + operator (it is used for addition and string concatenation).

    you can make your life easier in the month case:
    PHP Code:
    <select name="selMonth">
        <
    option value="0">Jan</option>
        <
    option value="1">Feb</option>
        <
    option value="2">Mar</option>
    // ...
    </select
    in the end, you just define a new Date object.
    PHP Code:
    var day  document.form1.selDate.value;
    var 
    mon  document.form1.selMonth.value;
    var 
    year document.form1.selYear.value;
    var 
    Birthday = new Date(yearmonday); 
    after that you only need to calculate the year portion out of the Date difference.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    coolecho (04-21-2010)

  • #3
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,091
    Thanks
    38
    Thanked 498 Times in 492 Posts
    Or check your other forum submissions ... see: http://www.webdeveloper.com/forum/sh...d.php?t=228388

  • Users who have thanked jmrker for this post:

    coolecho (04-21-2010)

  • #4
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hello,

    Sorry about the cross post. The .value worked great, and changing month selects to numbers as well.

    thanks


  •  

    Posting Permissions

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