# Javascript to display current age

Show 40 post(s) from this thread on one page
Page 2 of 2 First 12
• 04-26-2013, 03:39 PM
aaansuuu

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?
• 04-26-2013, 05:18 PM
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.
• 04-26-2013, 05:26 PM
aaansuuu
Thank you so much for the great explanation!!
Well I now know for sure I shouldn't yet change anything in Javascripts myself :)
• 04-26-2013, 05:27 PM
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
• 04-26-2013, 05:31 PM
aaansuuu
YES! It works! :)
Thank you bradymc, you really helped me out a lot :)
• 04-26-2013, 05:34 PM
• 04-27-2013, 07:43 AM
007Julien
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"> <meta name="generator" content="PSPad editor, www.pspad.com"> <title></title> <style type="text/css"> </style> </head> <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)...
• 04-27-2013, 09:15 AM
007Julien
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 !
Show 40 post(s) from this thread on one page
Page 2 of 2 First 12

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.09521 seconds
• Memory Usage 2,376KB
• Queries Executed 11 (?)
Template Usage (23):
• (2)bbcode_code_printable
• (1)footer
• (1)gobutton
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)pagenav
• (1)pagenav_curpage
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
Included Files (19):
• ./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/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (43):
• init_startup
• 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
• friendlyurl_resolve_class
• friendlyurl_clean_fragment
• friendlyurl_geturl
• pagenav_page
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates