www.webdeveloper.com
Results 1 to 3 of 3

Thread: JavaScript Age calculator problem

  1. #1
    Join Date
    Feb 2011
    Posts
    8

    JavaScript Age calculator problem

    I am trying to create a age calculator using selection lists. I need to have these question answered:

    User's age in years, months, and days;
    How long a person has been living in months, days, hours;
    Days till user's birthday
    I am not sure how to calculate it through date functions. How do I get this to work?

  2. #2
    Join Date
    Feb 2011
    Posts
    8

    code

    Here is what I have:

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
    >
    <
    html xmlns="http://www.w3.org/1999/xhtml">
    <
    head>
    <
    title>Age Calculator</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <
    script type="text/javascript">
    /* <![CDATA[ */
    unction submitForm() {
    /* d represents today */
    var = new Date();
    var 
    year d.getFullYear();
    var 
    month d.getMonth();
    var 
    days d.getDays();
    var 
    minutes d.getMinutes();
    var 
    hours d.getHours();

    var 
    byear document.forms[0].year.value;
    var 
    bmonth document.forms[0].month.value;
    var 
    bday document.forms[0].day.value;
    var 
    bhours bday 24;
    var 
    bmin 60 bhours;

    if (
    isLeapYear== true) {
                var 
    dyears year byear;
                var 
    dmonth month bmonth;
                var 
    ddays = (days bday) + 1;

                var 
    dhours hours bhours;
                var 
    dminutes minutes bmin;

                var 
    daysTillBDay 365 bday;
    }

        else (
    isLeapYear()== false) {
            var 
    dyears year byear;
            var 
    dmonth month bmonth;
            var 
    ddays = (days bday);

            var 
    dhours hours bhours;
            var 
    dminutes minutes bmin;

            var 
    daysTillBDay 365 bday;
    }

                    
    document.uyears.value =  dyears " years old.";
                    
    document.umonth.value =  dmonth " months old.";
                    
    document.udays.value =  ddays " days old.";

                    
    document.lmonth.value "The user has been living for " dyears " years.";
                    
    document.ldays.value "The user has been living for " ddays " days.";
                    
    document.lhrs.value "The user has been living for " dhours " years.";
                    
    document.lmin.value "The user has been living for " dminutes " days.";

                    
    document.bdays.value daysTilBDay " days left till your birthday.";



    //This is fucntion determines if the year is a leap year and to help determine the amount of days in a year.
    function isLeapYear() {
     if (
    year 400 == || (year 100 != && year == 0))
      return 
    true;
     else
      return 
    false;
    }



    /* ]]> */
    </script>
    </
    head>
    <
    body>
    <!--
    My form -->
     <!-- 
    This is where my input items are defined and created.-->
     <
    div class="form">
    <
    form action="" onsubmit="return submitForm();">
    <
    pPlease choose one item from each section for your birthday date
            <
    p>Day
            
    <select name="day">  
                <
    option value="0" selected="selected">[choose one]</option>
                        <
    option value="1">1</option>
                        <
    option value="2">2</option>
                        <
    option value="3">3</option>
                        <
    option value="4">4</option>
                        <
    option value="5">5</option>
                        <
    option value="6">6</option>
                        <
    option value="7">7</option>
                        <
    option value="8">8</option>
                        <
    option value="9">9</option>
                        <
    option value="10">10</option>
                        <
    option value="11">11</option>
                        <
    option value="12">12</option>
                        <
    option value="13">13</option>
                        <
    option value="14">14</option>
                        <
    option value="15">15</option>
                        <
    option value="16">16</option>
                        <
    option value="17">17</option>
                        <
    option value="18">18</option>
                        <
    option value="19">19</option>
                        <
    option value="20">20</option>
                        <
    option value="21">21</option>
                        <
    option value="22">22</option>
                        <
    option value="23">23</option>
                        <
    option value="24">24</option>
                        <
    option value="25">25</option>
                        <
    option value="26">26</option>
                        <
    option value="27">27</option>
                        <
    option value="28">28</option>
                        <
    option value="29">29</option>
                        <
    option value="30">30</option>
                        <
    option value="31">31</option>
            </
    select></p>
        

    <
    p>Month
            
    <select name="month">  
                <
    option value="0" selected="selected">[choose one]</option>
                        <
    option value="1">January</option>
                        <
    option value="2">Febuary</option>
                        <
    option value="3">March</option>
                        <
    option value="4">April</option>
                        <
    option value="5">May</option>
                        <
    option value="6">June</option>
                        <
    option value="7">July</option>
                        <
    option value="8">August</option>
                        <
    option value="9">Sepetmber</option>
                        <
    option value="10">October</option>
                        <
    option value="11">September</option
                        <
    option value="12">December</option>
            </
    select></p>
            
    <
    p>Year
            
    <select name="year">  
                <
    option value="0" selected="selected">[choose one]</option>
                <
    script type="text/javascript" >
                
    /* <![CDATA[ */
                //javascript loop for year selected
                    
    var today = new Date();
                    var 
    year =  today.getFullYear();
                    var 
    stopyears year 61;
                    for (; 
    stopyears <= yearstopyears++ ){
                        
    document.write("<option value=\"" stopyears "\" >" stopyears "</option>" );
                    }
                    
    /* ]]> */
                
    </script>
            </
    select></p
            <
    p><input type="submit" value="Calculate" /> 
    </
    div>
    <
    div class="results" align="right">

        <
    h4Results</h4>
            <
    h6>Users Age in:</h6>
                <
    pYear: <input type="text" name="uyears" /> </p>
                <
    pMonths: <input type="text" name="umonths" /> </p>
                <
    pDays: <input type="text" name="udays" /> </p>
        &
    nbsp;
            <
    h6>How long the user has been living:</h6>
                <
    pMonths: <input type="text" name="lmonths" /> </p>
                <
    pDays: <input type="text" name="ldays" /> </p>
                <
    pHours <input type="text" name="lhrs" /> </p>
                <
    pMinutes <input type="text" name="lmin" /> </p>
        &
    nbsp;
            <
    h6>Days till the user's next birthday: </h6>
                <p> Days: <input type="text" name="bdays" /> </p>
        
    </div>

    </form>
    </body>
    </html> 

  3. #3
    Join Date
    Oct 2003
    Location
    Andover, UK
    Posts
    950
    I wrote a plugin for this some time ago - see http://www.mitya.co.uk/scripts/Calcu...-two-dates-100

    Example - hours since birth (assuming birth was at 29/05/1980 17:37:00):

    Code:
    var hours = between2Dates('29/05/1980 17:37:00', 'now', 'hours');
    alert("You have been alive "+hours+" hours!");

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.14815 seconds
  • Memory Usage 2,954KB
  • Queries Executed 15 (?)
More Information
Template Usage (34):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_code
  • (1)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (3)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (3)postbit
  • (3)postbit_onlinestatus
  • (3)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (71):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates