Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Javascript for calculating End Date

Not applicable
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...
0 Replies
pguerett
Level 10
Level 10
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.
pguerett
Level 10
Level 10
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?
Not applicable
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?
pguerett
Level 10
Level 10
Give me your email address and I will mail you a sample
Not applicable
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")
_Win_Lam_
Level 2
Level 2
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
_Win_Lam_
Level 2
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?
pguerett
Level 10
Level 10
I will be away until next week .....I will respond when I return...then I will have time to help you.
_Win_Lam_
Level 2
Level 2
Ok and thanks Paul... You're a great help!
_Win_Lam_
Level 2
Level 2
Hi Paul, I wonder if you are back? I'm almost dead with this. 😞 Sorry..
pguerett
Level 10
Level 10
Just sent you a reply ...let me know if that is what you want.
_Win_Lam_
Level 2
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
johnqdaily
Level 4
Level 4
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
johnqdaily
Level 4
Level 4
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 ----------------
Not applicable
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
Not applicable
----- 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")