Expand my Community achievements bar.

SOLVED

Calculate Difference in Days Between two Dates

Avatar

Former Community Member

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.

Please advise.

1 Accepted Solution

Avatar

Correct answer by
Level 6

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.

View solution in original post

10 Replies

Avatar

Former Community Member

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));

Avatar

Level 6

Your calculation looks OK, what exactly is the problem?

Avatar

Former Community Member

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.

Avatar

Level 6

"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:

     app.alert(String(diffDays));

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.

Avatar

Former Community Member

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.

Avatar

Correct answer by
Level 6

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.

Avatar

Former Community Member

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)

Avatar

Level 6

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;

Avatar

Former Community Member

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;

I tried adding:

app.alert(String(diffDays));

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"

Avatar

Level 6

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