Expand my Community achievements bar.

Date Manipulation

Avatar

Former Community Member
What I am looking to do is subtract one date field from another to give me the difference. I had something along these lines written for a form designed in designer 5, I could change this script, however I would really like to use the calender functionality in 7, any pointers ?



Thanks
7 Replies

Avatar

Former Community Member
I've never tried this, but you could probably use the Date2Num Formcalc method on the two dates and then just subtract the two values to get the difference in days.



H.

Avatar

Former Community Member
Hubert,



Thanks for clue.

Here is the formcalc code for anybody else with a similar problem.



if (HasValue(start) and HasValue(end)) then

$ = Date2Num(end, "YYYY-MM-DD") - Date2Num(start, "YYYY-MM-DD")

else $ = null

endif

Avatar

Former Community Member
hi Thierry,



Can you please explain how you got the formCalc to work?

Which event are you supposed to put the code on etc?

i tried this script but couldn't get it to work.

i've never used formCalc before so not sure what everything meant.



i think i'm putting it in the wrong place?



thanks

Avatar

Former Community Member
As a reference, I thought I'd add the following to this thread - I tweaked Thierry's code to work on my form:



if (HasValue(Absent_From) and HasValue(Returned_On)) then

$ = Date2Num(Returned_On, "YYYY-MM-DD") - Date2Num(Absent_From, "YYYY-MM-DD")

else $ = null

endif



Ensure that the field type you ascribe the code to is a Numeric Field and that the trigger event is *Calculate



Unfortunately, I then realised I only wanted to take account of working days lost (Mon-Fri), so can't make use of this calculation.

Avatar

Former Community Member
Is it possible to display the return from the above calculation as years/months/days or years.fraction of years (e.g., 500 days = 1.369 years)?



thanks.

Avatar

Level 7
For a rough approximation, divide by 365.25.



For the whole shooting match in AcroForms JavaScript:



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, 28, 31,

30, 31, 30,

31, 31, 30,

31, 30, 31

);



// 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]; // 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



// get the age array element [0] is years, [1] is months, and [2] is days.

var aAge = DateDiff( new Date(), util.scand("mm/dd/yyyy", this.getField("birthdate").value) );

event.value = "Your age is: " + aAge[0] + " years " + aAge[1] + " months " + aAge[2] + " days.";



/* Example of date arithmetic. */

console.show();

console.clear();

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

var d1 = util.scand("mm/dd/yyyy", this.getField('birthdate').value);

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

var d2 = new Date();

/* Number of seconds difference. */

var diff = (d2.valueOf() - d1.valueOf()) / 1000;

/* Print some interesting stuff to the console. */

console.println("It has been " + diff + " seconds since " + util.printd("mm/dd/yyyy", d2));

console.println("It has been " + diff / 60 + " minutes since " + util.printd("mm/dd/yyyy", d2));

console.println("It has been " + (diff / 60) / 60 + " hours since " + util.printd("mm/dd/yyyy", d2));

console.println("It has been " +

((diff / 60) / 60) / 24 + " days since " + util.printd("mm/dd/yyyy", d2));

console.println("It has been " +

Math.floor(((diff / 60) / 60) / 24) + " days since " + util.printd("mm/dd/yyyy", d2));

console.println("It has been " +

Math.floor(((diff / 60) / 60) / 24) / 365 + " 365 day years since " + util.printd("mm/dd/yyyy", d2));

console.println("It has been " +

Math.floor(((diff / 60) / 60) / 24) / 365 + " 365.25 years since " + util.printd("mm/dd/yyyy", d2));



The above can be converted LiveCycel Desigenr JavaScript.



For a workin example:



http://forum.planetpdf.com/wb/default.asp?action=9&read=58659&fid=5#160601