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

Newbie to Javascript needs assistance with a simple code.

TM921
Registered: Apr 14 2009
Posts: 11
Answered

Hi all. I'm in the process of creating a form. I need simple Javascript code to calculate # of days in between two dates.

Thank you,

Thomas

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Have you looked at [url=http://www.acrobatusers.com/tutorials/2006/date_time_part1]Working with date and time in Acrobat JavaScript (Part 1 of 3)[/url] by Thom Parker?

Or the post about [url=http://www.acrobatusers.com/forums/aucbb/viewtopic.php?id=19419]Calculate age in javascript[/url]. You may need to adjust the final divison for the appropriate time interval.

George Kaiser

TM921
Registered: Apr 14 2009
Posts: 11
Thank you for that quick reply. Yes, I have. I've been tinkering with it for a day and a half and I just can't seem to get the kinks out.
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Without more information like the code, information about the field providing the data, and any JavaScript debugging console messages, it is hard to tell where the problem is. Note that one needs to match the format of the date field and the formatting string used by the 'util.scand()' method is very important. If it is not the same the conversion will not work, or it will be wrong.

With a form that has the following 3 fields with the following formats:

Starting date - Name: "StartDate" Format: "mm/dd/yyyy"
Ending date - Name: "EndDate" Format: "mm/dd/yyyy"
Difference in days - Name: "fDays" Format "Number" zero decimal places

The "custom calculation script" for the "fDays" field with debugging information:
console.show(); // force debugging displayconsole.clear(); // starting date informationvar sStartFormat = 'mm/dd/yyyy';var sStartDateName = 'StartDate'; // verify start date informationconsole.println("starting date format: " + sStartFormat);console.println("starting date field name: " + sStartDateName); // ending date informationvar sEndFormat = 'mm/dd/yyyy';var sEndDateName = 'EndDate'; // verify ending date informationconsole.println("ending date format: " + sEndFormat);console.println("ending date field name: " + sEndDateName); // get the field valuesvar sStartDate = this.getField(sStartDateName).value;var sEndDate = this.getField(sEndDateName).value; // verify date valuesconsole.println("starting date value: " + sStartDate);console.println("ending date value: " + sEndDate); // define some date time constantsvar fSec = 1000; //secondvar fMin = 60 * fSec; // minutevar fHr = 60 * fMin; // hourvar fDay = 24 * fHr; // day // process if there is dataif(sStartDate != '' & sEndDate != '') { // get start date number of days from the epoch date for the date and report progressvar oStartDate = util.scand(sStartFormat, sStartDate); // start date objectconsole.println('start date time object: ' + oStartDate);var fStartMillSec = oStartDate.valueOf(); // milliseconds since epoch dateconsole.println('start date in millisonds: ' + fStartMillSec);var fStartDays = fStartMillSec / fDay; // floating number of daysconsole.println('start date in days: ' + fStartDays);var iStartDays = Math.floor(fStartDays); // whole number of daysconsole.println('start date in whole days: ' + iStartDays); // get end date number of days from the epoch datevar oEndDate = util.scand(sEndFormat, sEndDate); // start date objectconsole.println('end date time object: ' + oEndDate);var fEndMillSec = oEndDate.valueOf(); // milliseconds since epoch dateconsole.println('end date in millisonds: ' + fEndMillSec);var fEndDays = fEndMillSec / fDay; // floating number of daysconsole.println('end date in days: ' + fEndDays);var iEndDays = Math.floor(fEndDays); // whole number of daysconsole.println('end date in whole days: ' + iEndDays); // compute difference in daysconsole.println('difference in days: ' + (iEndDays - iStartDays) );event.value = iEndDays - iStartDays;} else {event.value = '';}

George Kaiser

TM921
Registered: Apr 14 2009
Posts: 11
Thank you! That worked great. I'm also trying to do the following complex function. I want to be able to calculate 'Total Interest Due' = fDays * .0002739726 * PrincipalAmt but ONLY IF fDays > 90 or if CheckBox1 is checked then fdays > 180, otherwise tInterest = 0.See, I'm trying to create a form for my state judiciary where the bond forfeiture amount is to be calculated. Interest is only to be calculated on the bond amount from the fail to appear date (StartDate) if 90 days has elapsed OR 180 days has elapsed if an extension was granted. Either way, interest is to be calculated from StartDate.

Is this possible?
pranabeshpanda
Registered: May 8 2009
Posts: 30
Hi All,

I am right now using Adobe Acrobat 9 Pro version.
I do have a problem while formatting a date which has been input from the user end.

Ex :

In my form there is a field called "hdrETA_0001", which contains the date format "05-JUN-2008".
I am trying to read the value in the javascript like as below;

var estimateDate = this.getField("hdrETA_0001").value;

when I am trying to convert the date into different format (Ex. mm/dd/yyyy), its showing some different values.
app.alert(util.printd("dd/mm/yyyy",d));

Code :
/* Example of util.printd */
var estimateDate = this.getField("hdrETA_0001").value;
var s = "mm/dd/yyyy"
app.alert("After Formatting " + s + " looks like: "+ util.printd(s, estimateDate ));

Output : -3628/2273/-23920

Moreover when I do the same thing, taking current date, its showing proper date format.

/* Example of util.printd */
var d = new Date(); // Create a Date object containing the current date
var s = "mm/dd/yyyy"
app.alert("After Formatting " + s + " looks like: "+ util.printd(s, d));

Output : 03/06/2009 (i.e. today's date)

Could anybody help me?

Thanks & Regards,
Pranabesh

Thanks & Regards
Pranabesh

Freelancer
Registered: May 26 2009
Posts: 71
Hi,

you can do it something like this:

// have text field 'userDate' (formatted as dd-mmmm-yyyy)
// put this code under some button or so

// read the field userDate (aDate will be string)
var aDate = this.getField("userDate").value
app.alert("User Date string: "+aDate);

// this scand format HAS TO BE as field date format! (bDate will be object)
var bDate = util.scand("dd-mmmm-yyyy",aDate);
app.alert("User Date object: "+bDate);

// now you can format any valid date format (cDate will be string)
var cDate = util.printd("dd.mm.yyyy",bDate);
app.alert("User Date string formatted: "+cDate);

Hope this helps,

Freelancer

According to most IT HelpDesk people, the most common reason for user error (regardless of Operating System) is ID 10T.

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Pranabesh,

You are really starting a new post as the OP's question was answered.

JavaScript is an object orientated language and one needs to be careful about whether one is access an object, a property of an object or the result of applying a method of an object.

With your code:

Quote:
/* Example of util.printd */
var estimateDate = this.getField("hdrETA_0001").value;
var s = "mm/dd/yyyy"
app.alert("After Formatting " + s + " looks like: "+ util.printd(s, estimateDate ));
you are trying to apply the 'printd()' method to a date string, value of your field, and not a date object of the field as required by the documentation. So when you create the date object for the current date, the code will work because you have passed an object to the method and not the formatted string of the current date..

To show how this works try the following code:
// get the current date objectvar oNow = new Date();// display the current date objectconsole.println('current date object: ' + oNow);// show type ofconsole.println('typeof oNow:' + typeof oNow);// format current date object for displayvar sNow = util.printd('mm/dd/yyyy', oNow);// display formatted date stringconsole.println('formatted current date: ' + sNow);// typeof sNowconsole.println('typeof sNow: ' + typeof sNow);console.println('');// Try to format the current date string var sNowString = util.printd('mm/dd/yyyy', sNow);// display resultconsole.println('formatting of date string: ' + sNowString);console.println('');// let us try to process a date stringvar sMyDate = 'Jan 1, 1970';console.println('sMydate: ' + sMyDate);console.println('typeof sMyDate: ' + typeof sMyDate);// convert string to date objectvar oMyDate = util.scand('mmm d, yyyy', sMyDate);// display oMyDateconsole.println('oMyDate: ' + oMyDate);// type of oMyDateconsole.println('typeof oMyDate: ' + typeof oMyDate);// display object in various formatsconsole.println('oMyDate as mm/dd/yyyy: ' + util.printd('mm/dd/yyyy', oMyDate) );console.println('oMyDate as mmmm d, yyyy dddd: ' + util.printd('mm/dd/yyyy dddd', oMyDate));console.println('sMyDate as mmmm d, yyyy dddd: ' + util.printd('mm/dd/yyyy dddd', sMyDate));

You will need to use the '()' method to convert the date string value into a date object before using the 'printd()' method.

George Kaiser

pranabeshpanda
Registered: May 8 2009
Posts: 30
Thank you all for your valuable reply to my post :)
It worked fine for me. I was doing the thing wrongly.

I applied the technique whichever you guys given me. Today I was able to submit my module in time :)

Thanks a ton to all :)

Thanks & Regards
Pranabesh