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

Javascript Bug? Dates are not calculating correctly any more...

fmusignac
Registered: Nov 19 2009
Posts: 3
Answered

I have a form where you enter the pay beginning date and that populates the working days and weekends in the pay period. The form used to calculate correctly but now the dates for NOVEMBER and only NOVEMBER will not work. If I enter 11/01/2009, the first two days calculate as 11/1 and it will only display until the 14. If I do the same for any other month then it calculates correctly...

var strStart = this.getField("EmpInfo.startDate").value; gets the date entered
if(strStart.length)
{
var dateStart = util.scand("mm/dd/yyyy",strStart);
var oneDay = 24 * 60 * 60 * 1000;
var dueMillis = dateStart.getTime(); to calculate following dates I use dateStart.getTime() + 1 * oneDay substituting the 1 for the number of days Im trying to add.

var dueDate = new Date(dueMillis);
event.value = util.printd("m/d/yy",dueDate);
}

any help will be appreciated.. Im willing to share the form in exchange for some help. This used to work. I have last year's November form saved and the dates look right now when I reset the form and enter the same date (11/01/2008) it repeats 11/2!!!

My Product Information:
Acrobat Pro 9.2, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
I entered your code into a form and it all calculated correctly with the dates you specified.

Have you added any "console.println()" statements to this code to see what the intermediate values are?

You could also try adding an hour to the date value so that rounding errors won't cause it to cross an unwanted boundary.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
You might want to strip the strip the time form the input field and force it to the start of the day. I would also use a document level function so all the repeating code that does not change from field to field does not have to be edited in every field.
// document level JavaScripts for date manipulationvar PV_Second = 1000;var PV_Minute = 60 * PV_Second;var PV_Hour = 60 * PV_Minute;var PV_Day = 24 * PV_Hour; function Date2Num(cFormat, cDate) {// convert date in cFormat string to number of daysif(cDate.toString() == '') return '';var oDate = util.scand(cFormat, cDate);var fDate = oDate.valueOf();fDate = Math.floor(fDate / PV_Day) * PV_Day;return fDate / PV_Day;} function Num2Date(cFormat, fDays) {// convert number of days to cFormat stringif(fDays.toString() == '') return '';fDays = fDays * PV_Day;var oDate = new Date(fDays);return util.printd(cFormat, oDate);}

The the script for each field is reduced and there is only one value that needs to be updated, and that value could also be computed if needed.
// field custom calculation scriptvar nDays = 0; // change for days to addevent.value = ''; // set default value if no data processedvar iDays = Date2Num('mm/dd/yyyy', this.getField("EmpInfo.startDate").value);if (iDays.toString() != '') {event.value = Num2Date('m/d/yy', iDays + nDays);}}

George Kaiser

fmusignac
Registered: Nov 19 2009
Posts: 3
Thanks thomp and gkaiseril... I found that the hours were getting screwed up.. changed the calculation of the day to var oneDay = 25 * 60 * 60 * 1000 and it works...

Thanks again!