Expand my Community achievements bar.

Subtracting Dates in a Form

Avatar

Level 1

I'm a complete novice at form calculations.  I have successfully gotten some fields to subtract and multiply; however, I cannot get two fields to configure the number of days between two dates.  For instance, from July 1, 2010 to September 1, 2010, there are 62 days.  I then need this number to calculate the percentage of time for the year.

Any help is welcomed and appreciated!

*Using LiveCycle Designer 8.0

3 Replies

Avatar

Level 4

in a script object (I'll name Helper):

function calculateDateDifference(dateOne, dateTwo){

     var dOne = new Date(dateOne);

     var dTwo = new Date(dateTwo);

     var oneDay = 1000*60*60*24; //milliseconds in a day

     return Math.ceil((dTwo - dOne) / oneDay);

}

then you would just do:

var percentage = (Helper.calculateDateDifference("July 1, 2010", "September 1, 2010") / 365) * 100;

That should work...hope it helps

Avatar

Level 1

Aye.  I know this should be the answer, but I can't get it to work.

I put the script in, and remaned the fields, and no go.

Avatar

Level 1

Had to learn a little on my own, but eventually got it! Had to brush off my Java skills from 10 years ago (eeekkk!). Thanks for the help!