7 Replies Latest reply on Sep 14, 2011 5:24 PM by Walker.Cj3

    Find how many years and days between two dates

    Walker.Cj3

      Good Day,

       

      I've been tackling this problem for a couple of days now so I thought I would send this out and see if anyone can tell me what I'm doing wrong.

       

       

      /* Create a Date object with a definite date. */

       

      var d1 = getField("RELEASE.DATE").value;

       

      /* Create a date object containing the current date. */

       

      var d2 = getField("Text4").value;

       

      /* Number of years difference. */

       

      diff = (((((d2.valueOf() - d1.valueOf()) / 1000) /60) /60)/24)/365;

       

      //Displayed on field "Text3"

       

      getField("Text3").value =(diff);

      //<END>

       

      RELEASE.DATE value is 01/Jan/2011 using the date format dd/mmm/yyyy

      Text4 value is 01/Mar/2011 using the date format dd/mmm/yyyy

      The output to Text3 gives me a"NaN' result

       

      What I am trying to do is calcualted the number of years an employee has had with the company, then show a balance of days he has had in.

       

       

      Any thoughts would be greatly appreciated.

        • 1. Re: Find how many years and days between two dates
          gkaiseril MVP & Adobe Community Professional

          "NaN" stands for "Not a Number" and is usually the result of trying to do a mathematical operation on a string value and not a numeric value.

           

          You comment state you are getting the date object, but your code only gets the formatted date char cater string. You need to convert the formatted date string to a date object.

           

          Custom calculation for field Text3:

           

          // repeated conversion of date string to number of days for reuse of code

          function Date2Num(cFormat, cDate) {
          // convert formatted  date string to days since Epoch date
          var oDate = util.scand(cFormat, cDate);
          return Math.floor(oDate.getTime() / (1000 * 60 * 60 * 24));
          }

           

          // open debugging console
          console.show();console.clear();

           

          /* Create a Date object with a definite date. */
          var d1 = getField("RELEASE.DATE").value;

           

          // some information about retrieved value
          console.println("value d1: " + d1);
          console.println("typeof d1: " + typeof d1);
          console.println("valueof d1: " + d1.valueOf());

           

          // convert date string to date object
          var oD1 = util.scand("dd/mmmm/yyyy", d1);

           

          // display some information about date object
          console.println("value of date object: " + oD1);
          console.println("typeof for date object: " + typeof oD1);
          console.println("value of date object: " + oD1.valueOf());

           

          // get today's date from field
          var d2 = this.getField("Text4").value;

          // compute difference in days
          var diff = Date2Num("dd/mmm/yyyy", d2) - Date2Num("dd/mm/yyyy", d1);
          // convert days  to years
          diff = diff / 365.25;
          // display result
          event.value = diff;
          //<END>

          1 person found this helpful
          • 2. Re: Find how many years and days between two dates
            Walker.Cj3 Level 1

            Thank you very much,

             

            Coding works great.  I appreciate your help

            • 4. Re: Find how many years and days between two dates
              gkaiseril MVP & Adobe Community Professional

              One needs to round the result from the computation for the number years and the number of days in the partial year to the next lower integer by using the "Math.floor()"  or the "parseInt()' method.

               

              One may need to adjust the diff value if the end day is to be included in the time interval being computed.

              • 5. Re: Find how many years and days between two dates
                Walker.Cj3 Level 1

                So Here's what I have now,

                 

                I'm extremly new to programming language, so I appreciate your help

                 

                what would I place in the return.Math.Floor in order to round down the decmial  ie: if the answer was 11.86 I would want it to say 11

                 

                 

                // repeated conversion of date string to number of days for reuse of code

                 

                function Date2Num(cFormat, cDate) {
                // convert formatted  date string to days since Epoch date
                var oDate = util.scand(cFormat, cDate);
                return Math.floor(oDate.getTime() / (1000 * 60 * 60 * 24));
                String.format("%.0g%n");
                }

                 

                /* Create a Date object with a definite date. */
                var d1= (getField("Enrol Day").value + "/" + getField("EnrolMonth").value + "/" + getField("EnrolYear").value);

                 

                // convert date string to date object
                var oD1 = util.scand("dd/mmm/yyyy", d1);

                 

                // get today's date from field
                var d2 = (getField("TD.RelDay").value + "/" + getField("TD.RelMonth").value + "/" + getField("TD.RelYear").value);
                // compute difference in days
                var diff = Date2Num("dd/mmm/yyyy", d2) - Date2Num("dd/mmm/yyyy", d1);
                // convert days  to years
                diff = (diff / 365.25)   ;
                // display result
                event.value = diff;
                //<END>

                • 6. Re: Find how many years and days between two dates
                  gkaiseril MVP & Adobe Community Professional

                  // repeated conversion of date string to number of days for reuse of code
                  function Date2Num(cFormat, cDate) {
                  // convert formatted  date string to days since Epoch date
                  var oDate = util.scand(cFormat, cDate);
                  return Math.floor(oDate.getTime() / (1000 * 60 * 60 * 24));
                  }

                   

                  /* Create a Date object with a definite date. */
                  var d1= (getField("Enrol Day").value + "/" + getField("EnrolMonth").value + "/" + getField("EnrolYear").value);
                  // convert date string to date object
                  var oD1 = util.scand("dd/mmm/yyyy", d1);
                  // get today's date from field
                  var d2 = (getField("TD.RelDay").value + "/" + getField("TD.RelMonth").value + "/" + getField("TD.RelYear").value);

                   

                  // compute difference in days
                  var diff = Date2Num("dd/mmm/yyyy", d2) - Date2Num("dd/mmm/yyyy", d1);
                  // increment value if end date is to be inlcuded in calculation
                  // diff++;
                  // convert days  to whole years - truncate to next lower intiger
                  var nYears = Math.floor(diff / 365.25);
                  // get days for current year interval - remainder of diff days divided by 365.25
                  var nDays = Math.floor(diff % 365.25);

                   

                  // display inputted values
                  var cMsg = "End date:   " + d2 + "\t Compute days: " + Date2Num("dd/mmm/yyyy", d2);
                  cMsg +=  "\nStart date: " + d1 + "\t Compute days: " + Date2Num("dd/mmm/yyyy", d1);
                  // total difference in days
                  cMsg +=  "\nTotal elapsed days: " + diff;
                  // result in years and days
                  cMsg += "\nYears: " + nYears + " and " + nDays + " days";
                  app.alert(cMsg, 1, 0, "Results");
                  //<END>

                  • 7. Re: Find how many years and days between two dates
                    Walker.Cj3 Level 1

                    Thank you GKaiseril the script works great,

                     

                    I have learnt a lot through this script it's been very instrumental to myself while trying to learn JS.