Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    New Coder
    Join Date
    Aug 2014
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    New to Java ...Help Please

    Here is a script to calculate the age after entering the date of birth, how ever I cant seem to get it to account for the days , Only the months and years any ideas .. Thank you much


    var today = new Date() ;
    var dobf = this.getField("dob") ;
    var agef = this.getField("age") ;
    var dob = new Date(util.scand("mm/dd/yyyy",dobf.value)) ;

    var ddiff = 0;
    var mdiff = 0;
    var ydiff = 0;

    if (today.getDate() - dob.getDate() < 0) {
    ddiff = today.getDate() ;
    ddiff += new Date(today.getFullYear(), today.getMonth(), 0).getDate() - dob.getDate() ;
    mdiff-- ;
    } else {
    ddiff = today.getDate() - dob.getDate() ;
    }
    if (today.getMonth() - dob.getMonth() < 0) {
    mdiff += (today.getMonth() + (12 - dob.getMonth())) ;
    ydiff-- ;
    } else {
    mdiff += (today.getMonth() - dob.getMonth()) ;
    }
    ydiff += today.getFullYear() - dob.getFullYear() ;

    agef.value = ydiff ;

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,484
    Thanks
    23
    Thanked 636 Times in 635 Posts
    Java and JavaScript are two different programming languages. This is the area for DOM or JSON scripting using JavaScript. The Java section is located here Java and JSP

    But something to look at is
    var dobf = this.getField("dob") ; line two; (which uses a JAVA function 'getField') is also using 'this' which is not defined and line four is missing an ending bracket.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    947
    Thanks
    0
    Thanked 130 Times in 129 Posts
    If the birthday is entered in the format MM/DD/YYYY


    Code:
    Birthday=Inputfield.value;
    BirthDate=parseInt(BirthDay.substr(0,2));
    BirthMonth=parseInt(BirthDay.substr(3,2)) -1;
    BirthYear=parseInt(BirthDay.substr(6,4));
    Now=new date();
    NowDate=Now.getDate();
    NowMonth=Now.getMonth();
    NowYear=Now.getFullYear();
    
    Years=NowYear-BirthYear;
    if (NowMonth<BirthMonth || (NowMonth==BirthMonth && NowDate>BirthDate)){
    Years--;
    }
    agef.value=Years;

  • #4
    New Coder
    Join Date
    Aug 2014
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I apologize, its a JavaScript and it works fine with the exception that it would not calculate to the day , only the month.

    Thanks Leurura , I will try that one .

  • #5
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    947
    Thanks
    0
    Thanked 130 Times in 129 Posts
    Oops I made a "fatal" typo.
    Code:
    if (NowMonth<BirthMonth || (NowMonth==BirthMonth && NowDate>BirthDate)){
    should be:
    Code:
    if (NowMonth<BirthMonth || (NowMonth==BirthMonth && NowDate<BirthDate)){

  • #6
    New Coder
    Join Date
    Aug 2014
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Nope That did not work, my age is 0 ... regardless to the date input or the Date of Birth input ...

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,695
    Thanks
    80
    Thanked 4,511 Times in 4,475 Posts
    Are you aware you are missing stuff from the line
    Code:
        var dob = new Date(util.scand("mm/dd/yyyy",dobf.value ) );
    ?? Missing stuff shown in red.

    But what is util.scand? I would assume that is part of some utility package you are using, but how can we know if you are using it correctly? Oh, and what does scand() do if it encounters an improper value for a date string?

    And a hint: Just after that line, do
    Code:
        alert(dob);
    What does that show you? (It's called DEBUGGING.)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    947
    Thanks
    0
    Thanked 130 Times in 129 Posts
    Sorry I noticed that in two instances I have typed a single letter in lowercase where it should have been uppercase.
    And I also swapped BirthDate with BirthMonth when extracting them.

    Corrected and added to a funtion:
    Code:
    function getAge() {
    agef=document.getElementById('age');
    BirthDay=document.getElementById('dob').value;
    BirthDate=parseInt(BirthDay.substr(3,2));
    BirthMonth=parseInt(BirthDay.substr(0,2));
    BirthYear=parseInt(BirthDay.substr(6,4));
    Now=new Date();
    NowDate=Now.getDate();
    NowMonth=Now.getMonth();
    NowYear=Now.getFullYear();
    Years=NowYear-BirthYear;
    if (NowMonth<BirthMonth || (NowMonth==BirthMonth && NowDate<BirthDate)){
    Years--;
    }
    agef.value=Years;
    }
    And this time it is also tested

    And you can actually use any character as separator.
    Last edited by Lerura; 08-11-2014 at 04:01 AM.

  • Users who have thanked Lerura for this post:

    Gqman (08-11-2014)

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,695
    Thanks
    80
    Thanked 4,511 Times in 4,475 Posts
    To allow people to put in "3/7/1996" instead of having to do "03/07/1996", you can change the front end of that code easily:
    Code:
    function getAge() 
    {
        var agef = document.getElementById('age'); // ?? why not .value here?
        var dobi = document.getElementById('dob').value.split( "/" );
        if ( dobi.length != 2 ) { ...error in entry...tell them and do it again ... }
        var dobM = + dobi[0];
        var dobD = + dobi[1];
        var dobY = + dobi[2];
        ... etc...
    Lerura: It is considered good form in JavaScript to (a) put the keyword var in front of all variable declarations and (b) start local variable names (at least) with a lower case letter.

    If you omit the var inside a function, then the variable you assigned to (i.e., ALL the variables in the case of your code in post #8) becomes a GLOBAL variable! Don't pollute the global scope with variables that really should be local variables.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    947
    Thanks
    0
    Thanked 130 Times in 129 Posts
    Quote Originally Posted by Old Pedant View Post
    ?? why not .value here?
    Because agef is a reference to the output-field #age.
    and you can't do:
    Code:
        var agef = document.getElementById('age').value;
    
    .....
    
        agef=Years;

    Quote Originally Posted by Old Pedant View Post
    Lerura: It is considered good form in JavaScript to (a) put the keyword var in front of all variable declarations and (b) start local variable names (at least) with a lower case letter.

    If you omit the var inside a function, then the variable you assigned to (i.e., ALL the variables in the case of your code in post #8) becomes a GLOBAL variable! Don't pollute the global scope with variables that really should be local variables.
    (a) You are absolutely right! and I also normally do it. But my current family issues distracts me a lot and occationally causes such stupid errors.
    (b) You are also right here, even if it have no actual effect on the script, whether you start the variable names with upper or lowercase.

    And I can see that you have also have been a little to fast:
    Code:
     if ( dobi.length != 2 )
    should be
    Code:
     if ( dobi.length != 3 )
    Last edited by Lerura; 08-11-2014 at 05:23 AM.

  • Users who have thanked Lerura for this post:

    Gqman (08-11-2014)

  • #11
    New Coder
    Join Date
    Aug 2014
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I want to thank you guys for all your input, I'm using this JS in Acrobat PDF Form, where you enter todays date in field "date", then you enter the date of Birth in a field "dob" and when you hit tap the auto calculation result shows in Field "age" ..
    I hope this helped a little, its been 5 years plus since I took JS programming ... Thank you , I will try your way

    Thank you

  • #12
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    947
    Thanks
    0
    Thanked 130 Times in 129 Posts
    Quote Originally Posted by Gqman View Post
    .... where you enter todays date in field "date", ...
    then you can just add a block similar to the one extracting the birthday data, to extract todays data.

  • #13
    New Coder
    Join Date
    Aug 2014
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Still no "age" result I get 0 regardless to the "date" or "dob" input ...

    What I don't get is my first attempt works fine but is does not take the day in the calculation .

  • #14
    New Coder
    Join Date
    Aug 2014
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I think this line needs to be tweaked a bit
    ddiff += new Date(today.getFullYear(), today.getMonth(), 0).getDate() - dob.getDate() ;

  • #15
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    947
    Thanks
    0
    Thanked 130 Times in 129 Posts
    don't use your old code at all. It is errornous and unnecessarily complicated.

    Can you post your full page code, then I can take a look into what's causing the problem. (remember to use the # in the controlpanel to add it in a code block.
    Last edited by Lerura; 08-11-2014 at 06:11 AM.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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