10 Replies Latest reply on Mar 8, 2011 5:43 PM by Kevin Cavallo

# Calculate Difference in Days Between two Dates

Hi,

I'm trying to figure out how to calculate the difference in days between two dates using JavaScript in LiveCycle. (JavaScript knowledge = minimal)

Where "Start_Date" and "Current_Date" are the names of the two dates in the Hierarchy palette. (both Date/Time Field)

*Current date is using the Object > Value > Runtime Property > Current Date/Time

I need a Text or Numeric field displaying the difference in days. (Difference_in_Days)

I've noticed the following code being pretty standard amongst other responses:

var

Start_Date = new Date(Start_Date);

var

Current_Date = new Date(Current_Date);

var

nAgeMilliseconds = Current_Date.getTime() - Start_Date.getTime();

var

nMilliSecondsPerYear = 365 * 24 * 60 * 60 * 1000;

I know there's code missing, and the above code might not be correct.

• ###### 1. Re: Calculate Difference in Days Between two Dates

I tried using the below code (and other variations) to see if I could get any result in a field but came up without any.

var oneDay = 24*60*60*1000;

var firstDate = new Date(2008,01,12);

var secondDate = new Date(2008,01,22);

var diffDays = Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay));

• ###### 2. Re: Calculate Difference in Days Between two Dates

Your calculation looks OK, what exactly is the problem?

• ###### 3. Re: Calculate Difference in Days Between two Dates

I don't recieve a result when adding this code to a text or numeric field. (double checked to make sure JavaScript is selected as opposed to FormCalc)

The second example I referenced should be self containing, therefore have no requirements pertaining to the field the script that it was added to.

• ###### 4. Re: Calculate Difference in Days Between two Dates

"The second example I referenced should be self containing, therefore have no requirements pertaining to the field the script that it was added to."

Not sure what you mean by that.  What event did you add the script to?

Try adding the following debug line after the last line of the script:

It should pop up a window with the number of days.  If you get that, then the script is working fine but you are not assigning the value to a field correctly.

1 person found this helpful
• ###### 5. Re: Calculate Difference in Days Between two Dates

This is exactly what happened. I got an error stating "10" (the number of days between 2008,01,12 and 2008,01,22.

In terms of assigning the value incorrectly, my "event" is set on "calculate"

Under Objects > Value > Type is "Calculated - Read Only" ("Calculation Script" selected)

Under Objects > Field > Patterns I have no patterns set.

Under Objects > Binding > Default Binding set to "None"

Under Objects > Binding > Data Format set to "Float"

I'm not sure which of these are incorrect or if I've left out the limiting factor.

• ###### 6. Re: Calculate Difference in Days Between two Dates

OK, this is because of the way that javascript and the calculate event works.  The field will be filled with whatever the script resolves to at the end of execution. Technically, your script is not resolving to a value since the last thing that you do is an assignment to a variable.  Change the last line to read:

Math.abs((firstDate.getTime()

- secondDate.getTime())/(oneDay));

(eliminate the variable assignment) and get rid of the app.alert.  Your script will "return" (evaluate to) whatever the value of the calculation is and that will be stored in the field.

• ###### 7. Re: Calculate Difference in Days Between two Dates

Thanks!

Just wondering how to reference another date field in place of either or both of the dates.

Ex.

Instead of static dates:

var oneDay = 24*60*60*1000;
var firstDate = new Date(2008, 01, 22);
var secondDate = new Date(2008,01,12);
Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay));

...Reference fields:

var oneDay = 24*60*60*1000;
var firstDate = new Date(SubForm_DateField01);
var secondDate = new Date(DateField02);
Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay));

I also noticed that if the "firstDate" appears before the "secondDate" chronologically, you don't get a negative interger.

Is it possible when this circumstance happens to have a negative interger polulate? (vice - versa for a positive interger)

• ###### 8. Re: Calculate Difference in Days Between two Dates

To pull the data value from a field, you need to use the "rawValue" property of the field.  For example, if the name of your date field is SubForm_DateField01:

var firstDate = new Date(SubForm_DateField01.rawValue);

The Math.abs function is causing the negative number to become positive.  To fix, just remove that function so the line reads:

(firstDate.getTime() - secondDate.getTime()) / oneDay;

1 person found this helpful
• ###### 9. Re: Calculate Difference in Days Between two Dates

Where "DateField01" = user entered date field

Where "DateFiled02" = Current Date/Time (Runtime Property)

where "Subform" = the subform containing DateField01

My script now resembles:

var oneDay = 24*60*60*1000;
var firstDate = new Date(Subform.DateField01.rawValue);
var secondDate = new Date(DateField02.rawValue);
(firstDate.getTime() - secondDate.getTime()) / oneDay;

Although I assume the reason I didn't get an error is because DateField01 is empty when the form is opened.

If I swap in actual dates instead of fields it works perfectly.

When I use the fields I have no information populating after I enter a date in "DateField01"

• ###### 10. Re: Calculate Difference in Days Between two Dates

I sent you a private message.  If you can send me the form I can take a look at it.