6 Replies Latest reply on Jun 11, 2016 3:36 PM by stealthrs

    Rounding date of birth

    stealthrs

      I have 3 total text fields...

       

      The textfield called 'dob' is what will be populated in the format mm/dd/yyyy

       

      I want two other text fields to be populated automatically, 'age_exact' (where it always rounds down to the integer) and 'age_closest' (where it can round up to the integer of the next birthday)

       

      I'm really stuck trying to do this.  Any ideas?

        • 1. Re: Rounding date of birth
          Test Screen Name Most Valuable Participant

          Birthdays we understand exactly and simple date arithmetic can fail to match this, especially because of leap years.  If I have to work out age my approach is:

          - get year of birth, subtract from current year to get provisional age.

          - get month and day of birth, compare with today's month and day (do not use "day of year", compare separately)

          - if month&day is less subtract 1 from provisional age

          - if equal print happy birthday

          • 2. Re: Rounding date of birth
            stealthrs Level 1

            Good point about the Leap Years.  I don't know how to take part of the birthdate input value to do the arithmetic.(ie: to get 1955 pulled out of 05/05/1955)

             

            How would I write the calculation or javascript to get the year minus current year?  Once I have that baseline I can probably figure the rest out.

            • 3. Re: Rounding date of birth
              gkaiseril MVP & Adobe Community Professional



              If I have a field named "dob" and it has a format of "mm/dd/yyy" I can use the following script:

              var cDOB = this.getFeild("dob").value; // get the Date of Birth field value;

              var oDOB = util.scand("mm/dd/yyyy", cDOB);
              if(oDOB == null) app.alert("Invalid date string", 1, 0);
              if(oDOB != null)
              {
              // console.show();
              console.clear();
              console.println("For a date value of " + cDOB);
              console.println("Full year: " + oDOB.getFullYear());
              console.println("Zero based month: " + oDOB.getMonth());
              console.println("Date of month: " + oDOB.getDate());
              console.println("Time: " + util.printd("HH:MM:ss", oDOB));
              console.println("Date object string: " + oDOB);
              }

               

              We can get the current date object, including time, with a script like:

               

              var oNow = new Date();

               

              To compare this to your dob object we should adjust to the same time of day.

               

              ONow.setHours(0 , 0, 0, 0); // set time to midnight;

               

              With this information one should be able compute the difference in years, months, and days with some adjustment for months and dates before the month and date for the date of birth.

              • 4. Re: Rounding date of birth
                gkaiseril MVP & Adobe Community Professional

                The following function will compute the age in years, months, and days using the date objects for the start and end dates.

                 

                function getAge(fromdate, todate){
                /*
                purpose: compute the difference in years, months, and days between the fromdate object and the todate object;
                inputs: fromdate (starting) and todate (ending) date objects;
                returns formatted string of years, months, and days;
                */

                    // if todate parameter omitted use today's date;
                    if(typeof todate == "undefined")
                    {
                  // create new date object form current system date and time;
                  var todate = new Date();
                }
                // set todate to midnight;
                todate.setHours(0, 0, 0, 0);
                // set fromdate to midnight;
                fromdate.setHours(0, 0, 0, 0);
                    // define and initialize variables for this function;
                    var age = new Array(),
                    y = [todate.getFullYear(), fromdate.getFullYear()],
                    ydiff= y[0] - y[1],
                    m= [todate.getMonth(), fromdate.getMonth()],
                    mdiff= m[0]-m[1],
                    d = [todate.getDate(), fromdate.getDate()],
                    ddiff = d[0] - d[1];
                    aAgeString = new Array();
                // adjsut years, months, and days for todate months equal to fromdate and fromdate date equal to todate date;
                // adjusting for borrowed values;
                    if(mdiff < 0 || (mdiff === 0 && ddiff < 0))--ydiff;
                    if(mdiff < 0) mdiff += 12;
                    if(ddiff < 0){
                        fromdate.setMonth(m[1] + 1, 0);
                        ddiff= fromdate.getDate() - d[1] + d[0];
                        --mdiff;
                    }
                    // assign results to return array, 0 = years, 1 = months, 2 = days;
                    age[0] = ydiff;
                    age[1] = mdiff;
                    age[2] = ddiff;
                    return age;
                    /*

                    // optional display of age as an alert;
                    // create text string for result;
                    // if year diff not zero, the number of years, else null string;
                    // if(ydiff > 0) aAgeString.push(ydiff+ ' year'+(ydiff> 1? 's ' : ''));
                    // if month diff not zero, the number of months, else null string;
                    if(mdiff > 0) aAgeString.push(mdiff+ ' month' + (mdiff> 1? 's' : ''));
                    // if days diff not zero, the number of days, else null string;
                    if(ddiff> 0) aAgeString.push(ddiff+ ' day'+(ddiff> 1? 's' : ''));
                    // add "and" if more than 1 element before the last element;
                    if(aAgeString.length > 1) aAgeString.splice(age.length -1, 0, ' and ');
                    app.alert("The age is: " +  aAgeString.join(''));
                    */
                } // end getAge function;

                • 5. Re: Rounding date of birth
                  gkaiseril MVP & Adobe Community Professional

                  If you want the values when the difference in dates is expressed in decimal years:

                   

                  Code for calculation without any adjustment:

                   

                  var cDob = GetField(this, "dob").valueAsString;

                  var cNow = GetField(this, "now").valiueAsString;

                  event.value = ''; // clear result;

                  // compte only if we have data;

                  if(cDob != "" && cNow != "")

                  {

                  // get date objects;

                  var oDob = util.scand("mm/dd/yyyy", cDob);

                  var oNow = util.scand("mm/dd/yyyy", cNow);

                  // do not allow dobs after current date;

                  if(oNow.getTime() > oDob.getTime())

                  {

                    // compute age using the days in a year;

                    // milliseconds in a year;

                    var YearLength = 1000 * 60 * 60 * 24 * 365.2425

                    // end date in number of days;

                    var nNowDays = oNow.getTime() / YearLength;

                    // start date in number of days;

                    var nDobDays = oDob.getTime() / YearLength;

                    //

                    var nDiffDays = nNowDays - nDobDays;

                    event.value = nDiffDays;

                  }

                  else

                  {

                    app.alert("Dob can not occur after the current date!", 1, 0);

                  } // end dob after current date;

                  } // end values not null;

                   

                  Code for the next lower integer result:

                   

                  var cDob = GetField(this, "dob").valueAsString;
                  var cNow = GetField(this, "now").valiueAsString;
                  event.value = ''; // clear result;
                  // compte only if we have data;
                  if(cDob != "" && cNow != "")
                  {
                  // get date objects;
                  var oDob = util.scand("mm/dd/yyyy", cDob);
                  var oNow = util.scand("mm/dd/yyyy", cNow);
                  // do not allow dobs after current date;
                  if(oNow.getTime() > oDob.getTime())
                  {
                    // compute age using the days in a year;
                    // milliseconds in a year;
                    var YearLength = 1000 * 60 * 60 * 24 * 365.2425
                    // end date in number of days;
                    var nNowDays = oNow.getTime() / YearLength;
                    // start date in number of days;
                    var nDobDays = oDob.getTime() / YearLength;
                    //
                    var nDiffDays = nNowDays - nDobDays;
                    event.value = Math.floor(nDiffDays);
                  }
                  else
                  {
                    app.alert("Dob can not occur after the current date!", 1, 0);
                  } // end dob after current date;
                  } // end values not null;

                   

                  Code for rounding the result:

                   

                  var cDob = GetField(this, "dob").valueAsString;

                  var cNow = GetField(this, "now").valiueAsString;

                  event.value = ''; // clear result;

                  // compte only if we have data;

                  if(cDob != "" && cNow != "")

                  {

                  // get date objects;

                  var oDob = util.scand("mm/dd/yyyy", cDob);

                  var oNow = util.scand("mm/dd/yyyy", cNow);

                  // do not allow dobs after current date;

                  if(oNow.getTime() > oDob.getTime())

                  {

                    // compute age using the days in a year;

                    // milliseconds in a year;

                    var YearLength = 1000 * 60 * 60 * 24 * 365.2425

                    // end date in number of days;

                    var nNowDays = oNow.getTime() / YearLength;

                    // start date in number of days;

                    var nDobDays = oDob.getTime() / YearLength;

                    //

                    var nDiffDays = nNowDays - nDobDays;

                    event.value = Number(util.printf("%,101.0f", nDiffDays));

                  }

                  else

                  {

                    app.alert("Dob can not occur after the current date!", 1, 0);

                  } // end dob after current date;

                  } // end values not null;

                   

                  Code for the next higher integer:

                   

                  var cDob = GetField(this, "dob").valueAsString;
                  var cNow = GetField(this, "now").valiueAsString;
                  event.value = ''; // clear result;
                  // compte only if we have data;
                  if(cDob != "" && cNow != "")
                  {
                  // get date objects;
                  var oDob = util.scand("mm/dd/yyyy", cDob);
                  var oNow = util.scand("mm/dd/yyyy", cNow);
                  // do not allow dobs after current date;
                  if(oNow.getTime() > oDob.getTime())
                  {
                    // compute age using the days in a year;
                    // milliseconds in a year;
                    var YearLength = 1000 * 60 * 60 * 24 * 365.2425
                    // end date in number of days;
                    var nNowDays = oNow.getTime() / YearLength;
                    // start date in number of days;
                    var nDobDays = oDob.getTime() / YearLength;
                    //
                    var nDiffDays = nNowDays - nDobDays;
                    event.value = Math.ceil(nDiffDays);
                  }
                  else
                  {
                    app.alert("Dob can not occur after the current date!", 1, 0);
                  } // end dob after current date;
                  } // end values not null;

                  • 6. Re: Rounding date of birth
                    stealthrs Level 1

                    This was a lotttt more advanced than I would have ever imagined. Thanks for your input (clearly this took time).

                     

                    I can't get this to work, but I feel like it's because I'm not understanding a lot of things for many reasons.

                     

                    1) You have a few different sections you've coded for me over 3 posts..I'm not sure WHERE I would put each code.  Would they be custom calculations or JavaScripts, and not sure which textboxes needs to have them

                    2) I'm not sure if the code from your first AND second post are needed.  I believe they do...

                    3) I'm also not seeing the information ever being output to a textbox (like to age_closest or age_rounded) in your code, nor would I know fully which variable needs to be pulled over there.

                    4) This is beyond the complexity of what I normally do, lol

                     

                    P.S. In your most recent post, there's a few typos of "valiueAsString;" - I took out the 'i' and still couldnt get it to work.