How to count days from start and end date in pdf form?
Copy link to clipboard
Copied
Hi my question is how to write java script for conting days from start and end date?
I found and I use this script:
// Custom calculate script
(function () {
var sStart = getField("fill_9").valueAsString;
var sEnd = getField("fill_10").valueAsString;
var dStart, dEnd, diff;
if(sStart && sEnd) {
dStart = util.scand("dd/mm/yyyy", sStart);
dEnd = util.scand("dd/mm/yyyy", sEnd);
diff = dEnd.getTime() - dStart.getTime();
event.value = Math.floor(diff / 864e5)+1;
} else {
event.value = "";
}
})();
It's work but not correctly, if I put in fill_10 date less than fill_9 the result in fill_8 where is my counter is 0 or negative value for example -1 or -2 and so...
How can I restrict the counter to show appalert when the counter is 0 or negative?
And the user should enter the dates for fill_9 and fill_10 again?
Copy link to clipboard
Copied
Hi,
If you add a check in to make sure that dEnd is after dStart, then that should do, the code would be something like
if ( dEnd < dStart)
{
app.alert ( " end date must be after start date");
}
Hope this helps
Malcolm
Copy link to clipboard
Copied
Thank you, now alert message work.
// Custom calculate script
(function () {
var sStart = getField("fill_9").valueAsString;
var sEnd = getField("fill_10").valueAsString;
var dStart, dEnd, diff;
if(sStart && sEnd) {
dStart = util.scand("dd/mm/yyyy", sStart);
dEnd = util.scand("dd/mm/yyyy", sEnd);
diff = dEnd.getTime() - dStart.getTime();
event.value = Math.floor(diff / 864e5)+1;
} else {
event.value = "";
}
if ( dEnd < dStart)
{
app.alert ( " end date must be after start date");
}
})();
But can we restrict if this happend:
if ( dEnd < dStart)
{
app.alert ( " end date must be after start date");
}
The counter in fill_8 will be with null value and date in fill 10 will be null too?
And one more detail, how to set app alert message in fill 9 if the date is less than today date and also the value in field 9 will be null?
Copy link to clipboard
Copied
Hi,
If you check a field value against "" that is what they normally return when they have not been filled in, if I understand your instructions you don't want the code to run until fill_9, and fill_10 have all got a value, so you could add a check at the start of your script.
var sStart = this.getField("fill_9").valueAsString;
var sEnd = this.getField("fill_10").valueAsString;
if ( ( sStart != "") && ( sEnd != ""))
{
// your code goes here and will only be run when both fill_9 and fill_10 have values
}
In order to test against today just use the new Date() to gets todays date and then you can test against that exactly as you test the two dates your are currently working with.
Hope this helps
Malcolm
Copy link to clipboard
Copied
No, I want these fills 8 9 and 10 all they be empty if when user type date less than curent in start date and end date less than start date.
If the user type correct start and end date then calculate me how days are there.
If the date are incorrect then show me allert and don't fill the filles.
Is it possible?
Copy link to clipboard
Copied
HI,
Ok, something like this should solve the problem
// Custom calculate script
(function () {
var sStart = getField("fill_9").value;
var sEnd = getField("fill_10").value;
var dStart, dEnd, diff;
if(sStart && sEnd)
{
dStart = util.scand("dd/mm/yyyy", sStart);
dEnd = util.scand("dd/mm/yyyy", sEnd);
if ( dEnd < dStart)
{
app.alert ( " end date must be after start date");
event.value = "";
}
else
{
diff = dEnd.getTime() - dStart.getTime();
event.value = Math.floor(diff / 864e5)+1;
}
}
else
{
event.value = "";
}
app.alert ( " end date must be after start date");
}
})();
Just re-ordered your code a little.
Regards
Malcolm
[EDITED: to correct errors mentioned later in forum post ]
Copy link to clipboard
Copied
Error in this line is detected:
(function () {
Copy link to clipboard
Copied
The error is in the first line, actually... To add a comment one muse use two slashes, or a slash and an asterisk.
So change this:
/ Custom calculate script
To:
// Custom calculate script
Or simply remove that line altogether...
Copy link to clipboard
Copied
Now error in line 13 label 12:
{
Copy link to clipboard
Copied
An "invalid label" error?
Change this line:
event.value = "":
To:
event.value = "";
Also, you should delete one of the closing curly parentheses at the end of the code (line #26, for example).
BarlaeDC , for the future, it might be a good idea to run the code in the console before posting it, even without the actual file, just to make sure it doesn't have any syntax errors...
Copy link to clipboard
Copied
Hi,
try67​, you are correct I should test the code, I wrote that while traveling, and got a bit too confident.
No excuse really.
Regards
Malcolm
Copy link to clipboard
Copied
No worries. Happens to all of us...
Copy link to clipboard
Copied
In the end what can we do?
When user enter end date less than start date I want to see alert message "enter valid dates", I want also if the user enter invalid dates, then start date, end date fills and counter fill, All these values in these fills be empty.
Now your java script works but only app allert message when user put invallid dates if end date is less than start date.
But the values in start date and end date fills are writed and the counter is work with negative symbol -1 or -2 and so.
I don't want values in these fields (fills) when the dates are incorrect..
And we want the start date should be greater than curent date or equal to curent date, if the start date is less than current then we want allert message, and values there in dates fields (fills) and counter field (fill) be empty?
And in the end If we use your codes here in counter field to caluclate days from start and end dates my other calculation is broken related with counter field (fill)
If the counter is fill_8
For exampel I use script in fill_8 if counter value is 1 then write value 12 in fill_12, else write value 20?
I do it with this script:
var C = this.getField( "fill_8").value
if( C > 1 ) event.value = 20;
else event.value = 12;
It works before put in fill_8 this:
// Custom calculate script
(function () {
var sStart = getField("fill_9").valueAsString;
var sEnd = getField("fill_10").valueAsString;
var dStart, dEnd, diff;
if(sStart && sEnd) {
dStart = util.scand("dd/mm/yyyy", sStart);
dEnd = util.scand("dd/mm/yyyy", sEnd);
diff = dEnd.getTime() - dStart.getTime();
event.value = Math.floor(diff / 864e5)+1;
} else {
event.value = "";
}
if ( dEnd < dStart)
{
app.alert ( " end date must be after start date");
}
})();
Copy link to clipboard
Copied
Please help me with this., this is the last piece of my pdf form with java script adventure .
Copy link to clipboard
Copied
This code don't work correctly if start date is before 27/03/yyyy and end date is more than 27/03/yyyy.
I don't know why and how the code could be changed, so that code will calculate right.
Can someone help me to correct the code?
Copy link to clipboard
Copied
This is the day daylight-savings time enter into effect in many places, so the issue is very likely related to that.
Because the code relies on an exact hour difference between the two dates to calculate the time between them, and an extra hour is "lost" on that day, it can screw it up. The solution is to do a calculation that's based on full calendar days instead of on time, or to take that missing hour into consideration when performing the calculation (as well as the "extra" one added when daylight savings ends).
Copy link to clipboard
Copied
So the code should look like this than:
// Custom calculate script
(function ()
{
var sStart = getField("fill_9").value;
var sEnd = getField("fill_10").value;
var dStart, dEnd, diff;
if(sStart && sEnd)
{
dStart = util.scand("dd/mm/yyyy", sStart);
dEnd = util.scand("dd/mm/yyyy", sEnd);
if ( dEnd < dStart)
{
app.alert ( " end date must be after start date");
event.value = "";
}
else
{
diff = dEnd.getDate() - dStart.getDate();
event.value = diff}
}
else
{
event.value = "";
}
Copy link to clipboard
Copied
That seems better, yes.