Highlighted

Javascript for calculating End Date

Avatar

Guest

28-05-2008

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...

Replies

Highlighted

pguerett

28-05-2008

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.
Highlighted

pguerett

28-05-2008

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?
Highlighted

Avatar

Guest

28-05-2008

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?
Highlighted

Avatar

Guest

29-05-2008

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")
Highlighted

_Win_Lam_

25-06-2008

Hello... I bet we have something in common... 😛



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
Highlighted

_Win_Lam_

01-07-2008

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?
Highlighted