# Thread: JavaScript Age calculator problem

#### Hybrid View

1. Registered User
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. Registered User
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 d = 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 == 0 || (year % 100 != 0 && year % 4 == 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();"> <p> Please 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 <= year; stopyears++ ){                     document.write("<option value=\"" + stopyears + "\" >" + stopyears + "</option>" );                 }                 /* ]]> */             </script>         </select></p>          <p><input type="submit" value="Calculate" />  </div> <div class="results" align="right">     <h4> Results</h4>         <h6>Users Age in:</h6>             <p> Year: <input type="text" name="uyears" /> </p>             <p> Months: <input type="text" name="umonths" /> </p>             <p> Days: <input type="text" name="udays" /> </p>     &nbsp;         <h6>How long the user has been living:</h6>             <p> Months: <input type="text" name="lmonths" /> </p>             <p> Days: <input type="text" name="ldays" /> </p>             <p> Hours <input type="text" name="lhrs" /> </p>             <p> Minutes <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. Opera forever!
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!");```

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

#### Posting Permissions

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

"

"

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.13132 seconds
• Memory Usage 3,041KB
• Queries Executed 13 (?)
Template Usage (31):
• (1)bbcode_code
• (1)bbcode_php
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (3)memberaction_dropdown
• (1)navbar
• (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
Included Files (27):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.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 (72):
• 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_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• 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
• notices_check_start
• notices_noticebit
• process_templates_complete
• friendlyurl_redirect_canonical
• bbcode_fetch_tags
• bbcode_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