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.
Solved! Go to Solution.
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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));
Views
Replies
Total Likes
Your calculation looks OK, what exactly is the problem?
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
"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.
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.
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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)
Views
Replies
Total Likes
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;
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"
Views
Replies
Total Likes
I sent you a private message. If you can send me the form I can take a look at it.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies