These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Calculating number of Days between two dates

gsolorio3
Registered: Dec 10 2010
Posts: 14
Answered

I posted this yesterday and was given an article to read, which I did however the javascript is not working for me.
 
Basically, I have two datefields, start and end dates in date format (m/d/yyyy). I want to subtract the end date with the start date and add one, however I get "zero". Basically, I got this script from another posting but no response was given after it was given, whether it worked or not.
 
But here is the script I am currently using:
 
event.value = 0;var strStart = this.getField("StartDate").value;var strEnd = this.getField("DateWithdrawal").value;if(strStart.length & strEnd.length) {var Start1 = util.scand("m/d/yyyy",strStart);var End1 = util.scand("m/d/yyyy",strEnd);var diff = End1.getTime() - Start1.getTime();var oneDay = 24 * 60 * 60 * 1000;var days = Math.floor(diff/oneDay);event.value = days;}
  
I'm no expert in javascript so not sure if this is the right form or if I'm missing anything.
 
If someone can shed some light, would be nice.
 
Cheers,
Gilbert

My Product Information:
Acrobat Pro 10.0, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Where did you place that code, exactly? Which field and which event? Have you confirmed that the field calculation order is correct?

For what it's worth, the Adobe forums have been very flakey recently.
gsolorio3
Registered: Dec 10 2010
Posts: 14
I placed the code under the Calculate tab and under Custom Calculation script.


gsolorio3
Registered: Dec 10 2010
Posts: 14
i'm using a third field, where I want to subtract the start and end date. Looks like the field order is ok.


gsolorio3
Registered: Dec 10 2010
Posts: 14
although, i do get a warning javascript window - "Invalid date/time: please ensure that the date/time exists. Field[NumberofDays] should match format m/d/yyyy

And just checking the format of field "NumberofDays" is set to m/d/yyyy, just like the StartDate and EndDate.
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
You should not set the format of the NumberofDays field to a date format. Since it is a number, you can set it to a number format if you want, but it's really not necessary since it's a calculated field and the result will be an integer, so you can leave it without any format. Here's the script that I've revised and simplified a bit (and works for me):

// Custom calculate script
(function () {

var sStart = getField("StartDate").valueAsString;
var sEnd = getField("DateWithdrawal").valueAsString;
var dStart, dEnd, diff;

if(sStart && sEnd) {
dStart = util.scand("m/d/yyyy", sStart);
dEnd = util.scand("m/d/yyyy", sEnd);
diff = dEnd.getTime() - dStart.getTime();
event.value = Math.floor(diff / 864e5);
} else {
event.value = "";
}

})();


gsolorio3
Registered: Dec 10 2010
Posts: 14
Thank you George, you are a genious. It worked.
But now I need to add one day to the value.

For example, using your lovely script:

If the Start Date is 9/18/2010 and the Withdrawal Date is 10/5/2010, your script will give me a value of "17". But I need to add one day to the final calculation. So the answer should be "18".

I tried adding a "+1" to the following line:

Your line: diff = dEnd.getTime() - dStart.getTime();
My change: diff = (dEnd.getTime() - dStart.getTime()) + 1;

But that will not work. And since I don't know javascript, I thought I would simply add a "+1" but doesn't look like that is the proper way.

Secondly, when the person enters the Withdrawal Date (m/d/yyyy), is it possible to add a script under the Withdrawal Date field so that the date entered falls between the Start and End Date. If they enter a date that is outside the Start and End Date, a message will appeared letting them know.

Thank you so much.

Cheers,
Gilbert


George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Just add one to the final value:

// Add one day to the difference
event.value = Math.floor(diff / 864e5) + 1;


For the second question, is what you've been calling the withdrawal date the same as the end date? You seem to be using those names interchangeably (e.g., var sEnd = getField("DateWithdrawal").valueAsString;). Can you clarify how you've set it up?
gsolorio3
Registered: Dec 10 2010
Posts: 14
The Start Date and End Date fields are fixed dates. I actually set those days.
The Date Withdrawal is the date the user will enter in m/d/yyyy format. When they enter a date in that field (DateWithdrawal), I want the dates to be betwen the Start Date and End Date. So if the user enters a date that is outside that date range (between start and end date), the person is given a pop up message, informing them the date is invalid and must be between the Start and End date.



gsolorio3
Registered: Dec 10 2010
Posts: 14
just to clarify, I don't have any scripts in the DateWithdrawal field. I need some script that would only allow the user, when they enter a date, to fall between the Start and End date.
gsolorio3
Registered: Dec 10 2010
Posts: 14
Not sure if my message is confusing, might sound a little confusing but let's just say I have 3 dated fields:

Date1
Date2
Date3

For Date3 field, when a user enters a date, the date entered must be between Date1 and Date2.


George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Accepted Answer
Got it. Try something like this as the custom Validate script for the withdrawal field:

// Custom Validate script
(function () {

// Get the date field values
var sDate1 = getField("Date1").valueAsString;
var sDate2 = getField("Date2").valueAsString;
var sDate3 = event.value;

// Do nothing if field is empty
if (!sDate3) return;

// Construct an error message
var sMsg = "Please enter a date between " + sDate1 + " and " + sDate2 + "\r\rRight now!";

// Get the numeric representation of each date
nDate1 = util.scand("m/d/yyyy", sDate1).getTime();
nDate2 = util.scand("m/d/yyyy", sDate2).getTime();
nDate3 = util.scand("m/d/yyyy", sDate3).getTime();

// Show the error message if date is not within the range
if (nDate3 < nDate1 || nDate3 > nDate2) {app.alert(sMsg, 1);

//Optionally, reject the value
event.rc = false;
}

})();

George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
The forum software screwed up the code formatting, but you should get the idea.
gsolorio3
Registered: Dec 10 2010
Posts: 14
Thank you so much George. You are a life saver. It worked like a charm.
gsolorio3
Registered: Dec 10 2010
Posts: 14
George,

I was able to complete the PDF form. But I feel that I can simplied this part as well.

On one field I am calculating a percentage. I call this field PercentTermEnroll. This is calculated based on the number a student is enrolled divided by the total number of days in a term. This is working just fine.
But I have the user entering a dollar amount in field T_DSL (Total Award Received). In another field called R_DSL (Revised Total Award) I am basically doing a multiplication of two fields:

T_DSL*PercentTermEnroll

So if I enter $1,000 under field T_DSL and the percent is 9.6%, the revised total award (R_DSL) is $96

R_DSL = T_DSL*PercentTermEnroll

R_DSL = 1000*9.6% = 1000*0.096 = 96

But what I want to do is that if the percent is greater than 60.1%, the Revised Total Award (R_DSL) must be zero.

I basically put on the PDF form a note, that if the percent was greater than 60.1% to stop and not complete the form. But we may have students who don't read the form in its entirely and they will fill out the form. I would like a script that would put a zero if the percent was greater than 60.1%.

Thank you!