0 Replies Latest reply on Oct 18, 2012 8:09 PM by adrgall

    Date Difference function to display in months and years

    adrgall

      Hi,

       

      I am currently trying to call a date difference function that gives me an array with the number of years, months and days between two dates.

       

      I am wondering if someone can take a look at my document and let me know:

       

      - if the function is actually working correctly.

      - How to call the resulting array values into the objects in the form.

       

      Here is the datediff function:

       

      topmostSubform.#variables[0].DateCalc - (JavaScript, client)

       

      function DateDiff(oEndDate, oStartDate) {
      /*
      Purpose: compute the years, months and days that
      have elapsed between the passed start and end dates.

      Inputs:
      oEndDate - JavaScript date object for the end date
      oStartDate - JavaScript date object for the start date

      Returns: array containing the years, months and days that have elapsed.

      Notes: the JavaScript date/time object is used so the function
      does not need to resolve the various date formats.
      */

      // array for number days in a month for the current year - zero based array rows and columns
      var aDaysMonth = new Array(
      [31, "Jan", "January"],
      [28, "Feb", "February"],
      [31, "Mar", "March"],
      [30, "Apr", "April"],
      [31, "May", "May"],
      [30, "Jun", "June"],
      [31, "Jul", "July"],
      [31, "Aug", "August"],
      [30, "Sep", "September"],
      [31, "Oct", "October"],
      [30, "Nov", "November"],
      [31, "Dec", "December"]);

      // adjust number of days in a month for leap century and leap year
      if ((oEndDate.getYear() % 4) == 0) // leap
      aDaysMonth[1][0] = 29; // leap year then February has 29 days
      if (((oEndDate.getFullYear() % 400) != 0) & (oEndDate.getYear() == 0)) // check not leap century
      aDaysMonth[1][0] = 28; // if century is not a leap century then February has 28 days

      var endYear = oEndDate.getFullYear(); // get the full year from the end date object
      var endMonth = oEndDate.getMonth(); // get the zero based month from the end date object
      var endDate = oEndDate.getDate(); // get the date from the end date object

      var startYear = oStartDate.getFullYear(); // get the full year from the start date object
      var startMonth = oStartDate.getMonth(); // get the zero based month from the start date object
      var startDate = oStartDate.getDate(); // get the date from the start date object

      var diffYear = endYear - startYear;
      var diffMonth = endMonth - startMonth;
      var diffDate = endDate - startDate;

      // see if need to borrow from month
      if (diffDate < 0) {
      diffDate = diffDate + aDaysMonth[startMonth][0]; // add number of days for start date's month
      --diffMonth; // de cress start month by one
      }

      // see if need to borrow from Year
      if (diffMonth < 0) {
      diffMonth += 12; // add 12 months to diffMonth
      --diffYear; // decrease 1 year
      } // end if diffMonth < 0
      return [diffYear, diffMonth, diffDate]
      } // end function DateDiff

       

       

      And this is how I am trying to call the function in my form:

       

      var startDate = topmostSubform.Page1.SpecStartDate.rawValue;

      var endDate = topmostSubform.Page1.SpecToDate.rawValue;

      var aAge = topmostSubform.variables.DateCalc.DateDiff(endDate, startDate);

       

      this.rawValue = aAge[1];

      topmostSubform.Page1.SpecPeriodYears.rawValue = aAge[0];