Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

Javascript for calculating End Date

Avatar

Former Community Member
I have two date fields "Start_Date" and "End_Date". User selects the start date. Then the form takes that date and adds 365 days. puts the result in the "End_Date" field. I have attempted a couple of scripts and none have worked. So I attempted to first just get the selected date in the "Start_Date" field to show in the "End_Date" field. When I select a date like, "May 28,2008" the output reads "Object44194500". Using the following script in the "Start_date" field



----- form1.Order_Page.Product_Table.Row4.Subscription_Start::change: - (JavaScript, client) -------



xfa.resolveNode("form1.Order_Page.Product_Table.Row5.Subscription_End").rawValue = this;



What am I doing wrong. How do it get it show currently while at the same time adding 365 days. So if the user select May 28, 2008 the output in the "End_Date" field is May 27, 2009??? Thanks

- a frustrated new developer of LiveCycle Designer 8.0...
18 Replies

Avatar

Former Community Member
There are two ways top accomplish this.



1. Using Javascript - javascript has a date object but you will have to set your dat into the javascript object then ask for the year, add 1 to it then reassmble your date object. This date arithmetic would be documented in any javascript manual.



2. Using FormCalc - there are buikt in functions that allow you to do Date arithmetic. There is a function that will give you the number of days that have transpired from a set date in time. Once you have that you can add your 365 days to it then call the reverse function to turn it back into a proper date.



In either case it will get more complicated because you will have to deal with leap years to ensure that you get the right answer.

Avatar

Former Community Member
For setting one date to the other you will need to add a .rawValue on the end of the this statement. The way you have it coded now you are passing a complete object (by using "this" you are saying that you want the current object). The complete object can not be equated to the rawValue of the End_Date.



Make sense?

Avatar

Former Community Member
Paul,

I appreciate your response.



I tried using FormCalc:



In the "Start_Date" Field I did:



----- form1.Order_Page.Product_Table.Row4.Subscription_Start::change: - (FormCalc, client) ---------



form1.Order_Page.Product_Table.Row5.Subscription_End = ($ + 365)



Result was 365.



I am new with the program. Can you give me a little more detail?

Avatar

Former Community Member
Give me your email address and I will mail you a sample

Avatar

Former Community Member
Got it to work! Thanks. THis is what I did:



----- form1.Order_Page.Product_Table.Row4.Subscription_Start::exit: - (FormCalc, client) -----------



//initialize a field for storage

form1.Order_Page.TextField1.rawValue = 0



//debug code

//xfa.host.messageBox(form1.Order_Page.Product_Table.Row4.Subscription_Start.rawValue)



//Get the number of days that have transpired since the epoch

form1.Order_Page.TextField1.rawValue = Date2Num(form1.Order_Page.Product_Table.Row4.Subscription_Start.rawValue, "YYYY-MM-DD")



//Add 365 days to that number

form1.Order_Page.TextField1.rawValue = form1.Order_Page.TextField1.rawValue + 364



//debug message

//xfa.host.messageBox(Num2Date(form1.Order_Page.TextField1.rawValue, "YYYY-MM-DD"))



//Change the nunber back to a date and set enddate field

form1.Order_Page.Product_Table.Row5.Subscription_End.rawValue = Num2Date(form1.Order_Page.TextField1.rawValue, "YYYY-MM-DD")

Avatar

Level 2
Hello... I bet we have something in common... :P



Can we also have a script to validate that the Start_Date must be >=TODAY?



***Paul, is it ok for you to also share with me your sample mentioned above and guide me through this?

My email: win.welam@gmail.com

Avatar

Level 2
I've received your sample, thanks Paul! Can i check with you, can we have a script on the Calculate event at the end date so that it computes the end date automatically with Start Date + 365? Can you help me on this too?

Avatar

Former Community Member
I will be away until next week .....I will respond when I return...then I will have time to help you.

Avatar

Level 2
Ok and thanks Paul... You're a great help!

Avatar

Level 2
Hi Paul, I wonder if you are back? I'm almost dead with this. :( Sorry..

Avatar

Former Community Member
Just sent you a reply ...let me know if that is what you want.

Avatar

Level 2
Hi Paul, i'm so sorry...

But i don't seem to have received your reply... Is it by email?

win.welam@gmail.com

Avatar

Former Community Member
I was wondering if someone could take a look at my code below. I am trying to check two dates that users will enter. I have it set up so that the date is chosen from a drop down within the .pdf form.



I have been working on this for several hours and it's not working. UGH!!



-------Begin Code----------

topmostSubform.Page1.show_info.#area[0].exhibitToDate::change - (FormCalc, client)

var toDate = Date2Num(topmostSubform.Page1.show_info.#area[0].exhibitToDate.rawValue, "MM/DD/YYYY");

var fromDate = Date2Num(topmostSubform.Page1.show_info.#area[0].exhibitFromDate.rawValue, "MM/DD/YYYY");



if (fromDate > toDate) then

xfa.host.messageBox("Please check your End date. It cannot be before the Start date!", "Date Checker", 3);

endif;



-------End Code----------



I am running this in the change event. I have tried validation, exit, and many others and it doesn't work right.



It will give me an error no matter what the To Date is.



Anyone have thoughts? Thank you in advance!



John

Avatar

Former Community Member
I was able to find an answer on the LiveCycle Google Group. Here is what I was given. (for some reason I only got the FormCalc version to work).



-----------Begin Code--------------

if(EndDate.rawValue != null && StartDate.rawValue != null)

{

if(EndDate.rawValue < StartDate.rawValue)

{

xfa.host.messageBox("Please check your End date. It cannot be before

the Start date!", "Date Checker", 3);

this.rawValue = null;

// to set focus to the End Date field so that the user can type his

new value..

xfa.host.setFocus(this.somExpression);

}

}



Alternatively.. this must work if it is formcalc...



if(EndDate.rawValue <>null and StartDate.rawValue <> null)then

if(EndDate.rawValue < StartDate.rawValue) then

xfa.host.messageBox("Please check your End date. It cannot be before

the Start date!", "Date Checker", 3)

$.rawValue = ""

// to set focus to the End Date field so that the user can type his

new value..

xfa.host.setFocus(this.somExpression)

endif

endif



-------------End Code ----------------

Avatar

Former Community Member
Dustin,

Would you be willing to share the answer you received? It sounds as if we need the exact same information. I'm using this for an application...the user enters the effective date and I'd like to have the Expiration date auto fill with a date 1 year later.

Karen Claerbaut

Avatar

Former Community Member
----- form1.xxxxxxxxx.Subscription_Start::exit: - (FormCalc, client) -----------



//initialize a field for storage

form1.Order_Page.TextField1.rawValue = 0



//debug code

//xfa.host.messageBox(form1.xxxxxxxxxxxxxxx.Subscription_Start.rawValue)



//Get the number of days that have transpired since the epoch

form1.xxxxxxxxxxxxxxx.TextField1.rawValue = Date2Num(form1.xxxxxxxxxxxxx.Subscription_Start.rawValue, "YYYY-MM-DD")



//Add 365 days to that number

form1.xxxxxxxxxxxxxxxx.TextField1.rawValue = form1.xxxxxxxxxxxxxxxx.TextField1.rawValue + 364



//debug message

//xfa.host.messageBox(Num2Date(form1.xxxxxxxxxxxx.TextField1.rawValue, "YYYY-MM-DD"))



//Change the nunber back to a date and set enddate field

form1.xxxxxxxxxxxxxxxxxxxx.Subscription_End.rawValue = Num2Date(form1.Order_Page.TextField1.rawValue, "YYYY-MM-DD")