JS - Time comparison
Copy link to clipboard
Copied
Dear All,
as I am not specialist I kindly ask for your support. With my PDF form, which consits of 4 fields I want to track the actual project status based on date comparison (Process on schedule, in delay, finished on schedule, finished with delay). With dropdown (= field №1) menu user enters process status: Process NOT complete (export value = 0), Process complete (export value = 1). → Custom calcualtion script:
if (event.value=="1") {
this.getField("Date TODAY").value = util.printd("dd.mm.yyyy",new Date()); // Date, when user pushes dropdown → should not be automatically updated!
} else if (event.value=="0") {
this.getField("Date TODAY").value = this.getField("Date today").value; // Automatically updated date
}
This is not working correctly!!!
--------------------------------------------
Field for date comparison ("Time divergence"):
var v1 = this.getField("Deadline date).valueAsString;
var v2 = this.getField("Date TODAY").valueAsString;
if (v1 && v2) {
var d1 = util.scand("dd.mm.yyyy", v1);
var d2 = util.scand("dd.mm.yyyy", v2);
var diff = Math.round((d1-d2)/86400000);
event.value = diff;
} else event.value = "";
For some reasons status indication is not working correctly. I would be grateful for any advices / recommendations for correct JV.
Copy link to clipboard
Copied
Saying "it doesn't work correctly" is not very helpful... What exactly goes wrong with it?
Are there error messages? If so, what do they say?
Does it produce any output at all? If so, what output and how is it not correct?
Please elaborate.
Copy link to clipboard
Copied
@try67: Many thanks for your reply. The problem is, that "DATE TODAY"value, which is the basis for "Time divergence" evalution, is not updated correctly. Accordingly status indication is also not updated correctly. E.g.: Completed on schedule"
Based on the dropdown export value "0", "DATE TODAY" value should be automatically updated with system date.
Accordingly status indication field is also updated automatically. (E.g. "On schedule" can automatically switch to "Delay" dependend on the system date.)
Based on the dropdown export value "1", "DATE TODAY"value should be updated with date of users dropdown choice "Process complete" (export value = 1). Accordingly this date is static and should not be automatically updated. Accordingly status indication field should not be updated automatically.(E.g. "Completed on schedule" cannot automatically switch to "Completed with delay" dependend on the system date.)
Copy link to clipboard
Copied
It sounds like an issue with the fields calculation order. Make sure that the status field is further down the list than the "divergence" field. Also, remove this line from your code:
this.calculateNow();
Copy link to clipboard
Copied
...
Copy link to clipboard
Copied
Sorry, I'm not following. Where did you place the code? Can you share the file with us?
Copy link to clipboard
Copied
This is the dropdown menu code.
Copy link to clipboard
Copied
Complete code of dropdown
if (event.value=="1") {
this.getField("Date TODAY").value = util.printd("dd.mm.yyyy",new Date()); // Date, when user pushes dropdown → should not be automatically updated!
} else if (event.value=="0") {
this.getField("Date TODAY").value = this.getField("Date today").value; // Automatically updated date
}
Copy link to clipboard
Copied
That won't work. The "Date TODAY" field will not update automatically with the current system date if you do it like that. In order for that to happen you will need to add a doc-level script that changes the field's value each time the file is opened. Something like this:
if (this.getField("dropdown").value=="1") this.getField("Date TODAY").value = util.printd("dd.mm.yyyy",new Date());
(I don't know the name of the drop-down field, so I just used "dropdown"...)
Copy link to clipboard
Copied
Date is updating with the current system date for dropdown event value =1 . Thats the problem. It should not be updated automatically as user set process completed status.
if (event.value=="1") {
this.getField("Date TODAY").value = util.printd("dd.mm.yyyy",new Date()); // Date, when user pushes dropdown → should not be automatically updated!
} else if (event.value=="0") {
this.getField("Date TODAY").value = this.getField("Date today").value; // Automatically updated date
}
Copy link to clipboard
Copied
Dear try67,
many thansk for your advises. I proceeded as described by you before, but unfortunately it is still not correctly working for Process complete (export value = 1), where field for status indication should stay on "Completed on schedule", but switched during PDF opening to "Completed with delay". So this means, that date value is not static and changes based on system date.
Maybe in the b.m. script the util.printd("dd.mm.yyyy",new Date()); is the problem?
if (event.value=="1") {
this.getField("Date TODAY").value = util.printd("dd.mm.yyyy",new Date());

