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

susuns
Registered: Jul 7 2010
Posts: 11

I have entered the following to determine the number of days between two dates. The Beginning date field is called Start1, and the end date field is called End1 - the number of days field is called Traveldays1.

var strStart = this.getField("Start1").value;
var strEnd = this.getField("End1").value;
if(strStart.length & strEnd.length)
var Start1 = util.scand("dd/MM/YY",strStart);
var End1 = util.scand("dd/MM/YY",strEnd);
var diff = End1.getTime() - Start1.getTime();
var oneDay = 24 * 60 * 60 * 1000;
var days = Math.floor(diff/oneDay);
event.value = days;
else
event.value = 0;

The problem is that when I enter the dates in the files (Beginning date and End date) the Number of days field does not calculate. What am I doing wrong?

My Product Information:
Acrobat Standard 9.3.1, Windows
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
Do you get any errors in editing the script or running the script?

Your 'if' statement only executes the next line of code not the block of code you want because you have not marked the start and end of the block of code as JavaScript expects.

The picture "MM" is for minutes and 'YY' has no meaning when used with the 'util.scand' method.

George Kaiser

susuns
Registered: Jul 7 2010
Posts: 11
I must admit that I am very new to this and I actually copied this from another post - tell me how to fix it please!

All I am trying to do is determine the number of days between two dates that are manually entered. I would also like someway that the value would be zero (0) if there is nothing entered or the begin and the end dates are the same.

The Start date is titled Start1
the end date is titled End1
The numbers of days field is titled Traveldays1

Any help you could give me is greatly appreciated...
thanks!
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
You need to add the '{' and '}' to mark the blocks of code.
event.value = 0;var strStart = this.getField("Start1").value;var strEnd = this.getField("End1").value;if(strStart.length & strEnd.length) {var Start1 = util.scand("dd/mm/yy",strStart);var End1 = util.scand("dd/mm/yy",strEnd);var diff = End1.getTime() - Start1.getTime();var oneDay = 24 * 60 * 60 * 1000;var days = Math.floor(diff/oneDay);event.value = days;}

How are you counting the days leaving and retuning?
How are you counting travel days for the situation of returning on the day left?

You may have an issue with dates before the millennium.

George Kaiser

susuns
Registered: Jul 7 2010
Posts: 11
I need to actually add 1 to calculate the correct number of days in travel mode. I would liek the default to be zero if there are no entries in the start and end dates.
susuns
Registered: Jul 7 2010
Posts: 11
I have entered the code as you wrote it, however, when I enter the dates in the Start and end date fields- the answer does not appear in the days of travel field...is there something else I need to do?
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
Without seeing your form that can be hard to answer.

You must use the same date format for the field that you use in the script. The field names used in the script have to match the field names of your form with the same spelling and capitalization.

Are you getting any errors in Acrobat's JavaScript debugging console?

George Kaiser

susuns
Registered: Jul 7 2010
Posts: 11
I have set the date format to be mm/dd/yy in both the Start1 and End1 fields...(those are the names that are assigned to them - I think they are the same as listed in the code.
I have entered this code in the Custom calculation script on the calculate tab.. the format for the Traveldays1 field is a number....do I need to format that differently?