www.webdeveloper.com
Page 2 of 2 FirstFirst 12
Results 16 to 23 of 23

Thread: Javascript to display current age

  1. #16
    Join Date
    Apr 2013
    Posts
    4
    Thank you for your answer!

    It's difficult for me to understand how it actually works, so I wanted to ask about this line:

    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. #17
    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. #18
    Join Date
    Apr 2013
    Posts
    4
    Thank you so much for the great explanation!!
    Well I now know for sure I shouldn't yet change anything in Javascripts myself

  4. #19
    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 06:31 PM.

  5. #20
    Join Date
    Apr 2013
    Posts
    4
    YES! It works!
    Thank you bradymc, you really helped me out a lot

  6. #21
    Join Date
    Aug 2006
    Location
    Dallas area
    Posts
    57
    Your welcome. Glad we got it fixed.

  7. #22
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,272
    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)...
    Last edited by 007Julien; 04-27-2013 at 09:06 AM. Reason: complements...

  8. #23
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,272
    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 10:38 AM.

Thread Information

Users Browsing this Thread

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
  •  
HTML5 Development Center



Recent Articles