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

Livecycle Date calculations

Mike_
Registered: Feb 22 2010
Posts: 4

Hi all,

I'm having some troubles with date fields. I'm using Livecycle Designer ES 8.2.1.x.x.x.x and programming in JavaScript

What I'm trying to achieve is to have the user enter in the start date of a contract, after specifying the length (drop down list of months), and it automatically calculates the end date of the contract.

For example:

User selects 36 months,
User selects start date of 20th February 2010
The form should automatically calculate the end date as 19th of February 2013.

At the moment I have 3 fields:

termLength
startDate
endDate

I have so far managed to calculate the milliseconds of the term.

I am calculating on field exit and setting the rawValue of read-only fields for the result.

What I think I should be doing is obtaining the millisecond value of the start date, adding the term length, and then converting back to a date format. Unfortunately I can't seem to get the millisecond value of the entered date, and nor do I know how to convert a millisecond value back to date format.

Any help would be greatly appreciated!

- Michael

My Product Information:
LiveCycle Designer, Windows
Mike_
Registered: Feb 22 2010
Posts: 4
Bump.

Thought I'd add in some more info:

both startDate and endDate are date/time fields.


My current, unsuccessful code is as follows:

 form1.subform1.startDate::exit - (JavaScript, client) var d1 = this.rawValue;//I know that I don't want to use the rawValue, I want to use .getTime() or something var days = 30.416 * this.xfa.form.form1.subform1.termLength.rawValue;// This figure is 365 (days in a year) divided by 12 (months in a year). I multiply it by the term length to obtain the length of the term in days. var oneDay = 24 * 60 * 60 * 1000;// Number of milliseconds in a day var totalMS = days * oneDay;// Total milliseconds in the term this.xfa.form.form1.subform1.endDate.rawValue = d1 + totalMS;//again, probably should be using .setTime() or similar instead of rawValue

I know that the millisecond calculations work. I've tested them by outputting to a test text field.

I would really love some help on this!
Mike_
Registered: Feb 22 2010
Posts: 4
Thanks for all your help!

I've managed to fix the problem. I suspect there may be a quicker way but here is what I came up with:

 form1.subform1.startDate::exit - (JavaScript, client) var dateString = this.rawValue;var dateArray = dateString.split("-");//load date figures into an array var newDate = new Date(); // create new Date objectnewDate.setFullYear(dateArray[0], dateArray[1], dateArray[2]); // load date figures into Date object var days = 30.416667 * this.xfa.form.form1.subform1.termLength.rawValue;// This figure is 365 (days in a year) divided by 12 (months in a year). I multiply it by the term length to obtain the length of the term in days.var oneDay = 24 * 60 * 60 * 1000;// Number of milliseconds in a dayvar totalMS = days * oneDay;// Total milliseconds in the term var datems = newDate.getTime(); // get milliseconds of startDatevar newdatems = datems + totalMS; // calculate milliseconds of endDatevar newerDate = new Date(newdatems); // create Date object for endDate this.xfa.form.form1.subform1.endDate.rawValue = newerDate.getDate() + "/" + newerDate.getMonth() + "/" + newerDate.getFullYear();// output endDate figures into text field

My biggest problem was figuring out the javascript Date object, namely learning that I'm better off creating it blank and then using setFullYear(), as opposed to creating a Date object with parameters.

First got some output data with .getDay() and after a bit of wrestling and spending some time on javascript doco on the above I figured it out.

Hopefully this helps someone else down the track.
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
You can use the Acrobat JavaScirpt [url=http://livedocs.adobe.com/acrobat_sdk/9.1/Acrobat9_1_HTMLHelp/wwhelp/wwhimpl/js/html/wwhelp.htm?href=JS_API_AcroJSPreface.87.1.php&accessible=true]'util.scand()'[/url] method to get the JavaScript date object and the [url=http://livedocs.adobe.com/acrobat_sdk/9.1/Acrobat9_1_HTMLHelp/wwhelp/wwhimpl/js/html/wwhelp.htm?href=JS_API_AcroJSPreface.87.1.php&accessible=true]'util.printd()'[/url] method to format the output string.
// get the date time object from the formmatted stringvar oDate = util.scand("dd/mm/yyyy", startDate.formattedValue);// add term months to month of datevar fNewMonth = oDate.getMonth() + termlength.rawValue// make new date time object  with number of months added to monthvar oNewDate = new Date(oDate.getFullYear(), fNewMonth , oDate.getDate() );// display new date time object as a formatted string$.formattedValue = util.printd("dd/mm/yyyy", oNewDate);

Note the above code is JavaScript for LiveCycle Designer.

You can find out more about the 'util' object in the [url=http://livedocs.adobe.com/acrobat_sdk/9.1/Acrobat9_1_HTMLHelp/wwhelp/wwhimpl/common/html/wwhelp.htm?context=Acrobat9_HTMLHelp&file=JS_API_AcroJSPreface.87.1.php]Acrobat JavaScirpt API Reference[/url] and the JavaSript date object in the [url=https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference]Core JavaScript 1.5 Reference[/url] [url=https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Date]Date[/url]

George Kaiser

thibosshere
Registered: Jul 12 2010
Posts: 7
Sir,
I would like to need code like how to subtract two DATE fields for example

DATE FIELD 1 - DATE FIELD 2 = TextBox with this format 12-12-12(YY-MM-DD) how to do this in Adobe LiveCycle Designer.
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
You can use the 'Date2Num' function to obtain the values for the calculation.

George Kaiser