Copy link to clipboard
Copied
I have prepared a test form in which there are two date fields, namely "dt_start" and "dt_end" formatted as "dd-mmm-yy". Now I would like to show the date difference in another text field named "txt_dateDiff". I have added the following javascript codes in custom calculation field:
// Get the input field values
var s1 = getField("dt_start").valueAsString;
var s2 = getField("dt_end").valueAsString;
if (s1 && s2) {// Convert date strings to date objects
var d1 = util.scand("mm/dd/yyyy", s1);
var d2 = util.scand("mm/dd/yyyy", s2);
// Set this field value to the difference in days
// 864e5 = 86400000 = 1000 * 60 * 60 * 24 = number of milliseconds in a day
event.value = Math.abs(Math.round((d2 - d1)/864e5));
} else {
// Alert user
event.value = "Enter both date";
}
But unfortunately, nothing is shown on the text field, even no alert (Enter both date) is there. I have uploaded the test pdf to cloud and attaching the link here with this my post for your convenience.
https://www.dropbox.com/s/uhore9dhut7ykh4/testform.pdf?dl=0
Please let me know where the problem is.
Copy link to clipboard
Copied
Why does you use in the script a different date format?
Copy link to clipboard
Copied
I found this code by googling and thought this date format is not the data source date format, this format is the output date (date which will be shown in the text field) format. I might be wrong. Do you think, changing this format (mm/dd/yyyy) will resolve the issue? Please see my link where I have uploaded a copy of the form. You may kindly check the code there as well.
Thanks
Copy link to clipboard
Copied
Try it out.
Copy link to clipboard
Copied
Tried but no improvement. I have updated the pdf form according to your suggestion. Here is the updated link:
Copy link to clipboard
Copied
There are errors in your code. Check the JS Console...
Copy link to clipboard
Copied
There was no error except no "this". But still no solution after using "this" infront of "getField". Any idea?
Copy link to clipboard
Copied
There is most certainly an error:
Copy link to clipboard
Copied
Please tell me what is the solution. I have little knowledge about debugging!
Copy link to clipboard
Copied
Change the incorrect field names you used in your code.
Copy link to clipboard
Copied
I somehow resolved the problem but don't know what was the problem, really! The field names were not the problem although I have changed it to "dt_s" and "dt_e" later. Very peculiar.
Copy link to clipboard
Copied
You must use the correct names of the fields: