# Thread: Javascript to display current age

if (months%12 > 5 && years<14){

Is it correct or are some marks wrong here?
I'm still trying to fix the symbol, but don't know where it could come from. Maybe you could help me find out?

2. Registered User
Join Date
Aug 2006
Location
Dallas area
Posts
57
That line of code is checking for 2 conditions. That's what the "&&" characters are testing... to see that both conditions qualify to execute the code that follows.

Condition 1
if (months%12 > 5)
The percent sign (%) is calculating the remainder of division between "months" and the number 12 and seeing if it's greater than 5.
Example 1 - If months=18, the result is 6... because 18 divided by 12 is 1 with 6 leftover. 6 is greater than 5 so that part of the statement is true.
Example 2 - If months=86, the result is 2... because 86 divided by 12 is 7 with 2 leftover. 2 is not greater than 5 so this would be false.

Condition 2
if (years < 14)
14 was the designation to stop showing halves of years in the age. This can be changed to whatever age you like. But this means that you can be 12 1/2 years old, 13 1/2 years old, but when you get to 14, it's just 14 until you turn to 15 and so on.

So in putting these conditions to together, it's saying... If the age is less than 14 and the division remainder is greater than 5, then show their age with a "1/2" in it... 12 1/2, 13 1/2, or whatever the case may be. And if BOTH of those conditions are not met, it skips the following code and moves on down the list.

That's a LOOOONG way of saying that I don't believe that line of code is causing any issues. Let me compare what my last revision was to my file and compare it to what is shown and I'll post my results in a moment.

3. Thank you so much for the great explanation!!
Well I now know for sure I shouldn't yet change anything in Javascripts myself

4. Registered User
Join Date
Aug 2006
Location
Dallas area
Posts
57
I think I know what the problem is...
The "½" that is shown in the code is a special character and the browser can't interpret it unless you use an HTML entity. That's why you get the strange character. So your choices are...
Every time you see "½", you need to retype it as "1/2".
... or ...
Replace "½" with either the entity number or the entity name. Since it's difficult to post these entities without the browser changing them to the "½" symbol again, refer to this page about halfway down: http://www.w3schools.com/tags/ref_entities.asp
Last edited by bradymc; 04-26-2013 at 05:31 PM.

5. YES! It works!
Thank you bradymc, you really helped me out a lot

6. Registered User
Join Date
Aug 2006
Location
Dallas area
Posts
57

7. Registered User
Join Date
Oct 2010
Location
Versailles, France
Posts
1,290
Use the date object. Just from the current date and subtract days, months and years to find the date of birth.
Code:
```<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title></title>
<style type="text/css">

</style>
<body>
<p id="rsp"></p>
<script type="text/javascript">
var brd=new Date(1979,8,17,12);// year, javascript Month (from 0 up to 11), date at noon
var tdy=new Date();tdy.setHours(12); // at noon !

var dltDays=0;while (tdy.getDate()!=brd.getDate()) {dltDays++;tdy.setDate(tdy.getDate()-1);}
var dltMonths=0;while (tdy.getMonth()!=brd.getMonth()) {dltMonths++;tdy.setMonth(tdy.getMonth()-1);}
var dltYears=0;while (tdy.getFullYear()!=brd.getFullYear()) {dltYears++;tdy.setFullYear(tdy.getFullYear()-1);}
document.getElementById('rsp').innerHTML='Your are '+dltYears+' year'+(dltYears<2?'':'s')+' '+dltMonths+' month'+(dltMonths<2?'':'s')+' '+dltDays+' day'+(dltDays<2?'':'s')+' old';

</script>
</body>
</html>```
EDIT : NO Sorry ! The decline in months is wrong when the previous date does not exist (for example March 30th => 1 or 2 March)...
Last edited by 007Julien; 04-27-2013 at 08:06 AM. Reason: complements...

8. Registered User
Join Date
Oct 2010
Location
Versailles, France
Posts
1,290
We have only to work at the first day of months to avoid changing month :
Code:
```var brd=new Date(1980,0,30,12);// year , javascriptMonth( from à up to 11), date at noon
// The date with year, Javascript Month and date for test
var tdy=new Date(2013,1,28);tdy.setHours(12);
// Only to display the dates
var str='To day : '+tdy.toLocaleString()+'<br>Birthday : '+brd.toLocaleString()+'<br>';
var dltDays=0;while (tdy.getDate()!=brd.getDate()) {dltDays++;tdy.setDate(tdy.getDate()-1);}
// First day of the month
tdy.setDate(1);brd.setDate(1);
var dltMonths=0;while (tdy.getMonth()!=brd.getMonth()) {dltMonths++;tdy.setMonth(tdy.getMonth()-1);}
var dltYears=0;while (tdy.getFullYear()!=brd.getFullYear()) {dltYears++;tdy.setFullYear(tdy.getFullYear()-1);}

str+='Your are '+dltYears+' year'+(dltYears<2?' ':'s ')+' '+dltMonths+' month'+(dltMonths<2?'':'s')+' '+dltDays+' day'+(dltDays<2?'':'s')+' old';
document.getElementById('rsp').innerHTML=str;```
It would still not write null values...

EDIT : If you are born on March 31th 1980, Your are 33 years, 2 months and 45 days old on July 15th 2013 !
Last edited by 007Julien; 04-27-2013 at 09:38 AM.

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.17766 seconds
• Memory Usage 2,923KB
• Queries Executed 13 (?)
Template Usage (36):
• (2)bbcode_code
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (8)memberaction_dropdown
• (1)navbar
• (1)navbar_moderation
• (1)navbar_noticebit
• (1)navbar_tabs
• (2)option
• (1)pagenav
• (1)pagenav_curpage
• (8)postbit
• (8)postbit_onlinestatus
• (8)postbit_wrapper
• (1)spacer_close
• (1)spacer_open
• (1)tagbit_wrapper

Phrase Groups Available (6):
• global
• inlinemod
• postbit
• posting
• reputationlevel
Included Files (26):
• ./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
• pagenav_page
• pagenav_complete
• tag_fetchbit_complete
• forumrules
• navbits
• navbits_complete