Calculating a new date: adding days then subtracting a variable.

AL0001

21-07-2017

Hello:  I'm a newbie to javascript and have a little experience with LiveCycle.

I'm working on a form which will allow an applicant to calculate an approximate graduation date if they're getting an Associates Degree.  They enter their start date (Start_Date) and total credits earned to date in another field.  The Credits field returns a variable which reduces the graduation date.  So, I need to take the Start_Date, add 1068 days (which is 35 months converted roughly to days), then subtract the variable calculated from the Credits field and display an estimated graduation date.  I have tried many different ways of doing this based on examples I've found, and though LiveCycle says the syntax is OK, nothing will display in the field.  I hope this makes sense.  I'm taking a relatively simple Excel "calculator" and adding it to this form, but I just don't know enough about Javascript to get it to work. 

I hope this makes sense.  I need:

1. User enters Start Date & Credits Earned

2. Associates Degree field would calculate Start Date + 1068 days (~35 months from the date entered)

3. If any credits have been earned to date, a variable (converting credits earned to a number of days, which I already have calculating correctly in a hidden field) would be subtracted

Start Date + 1068 days - Variable = Graduation Date

Any help would be appreciated.

Accepted Solutions (0)

Answers (1)

Answers (1)

radzmar

MVP

24-07-2017

Hi,

you can calculate dates much easier with FormCalc that with JavaScript.

Here's all you need, so far:

if (not $.isNull) then

var cPattern = "DD.MM.YYYY" ;How the entered date is formatted

var cStartDate = $.formattedValue ;The entered anf formatted date

var iStartDate = Date2Num(cStartDate, cPattern) ;The date converted into an integer

var iNumMonths = 35 ;Months to count

var iNumSubtractDays = 7 ;Value to be subtracted later.

var iNumCount = 0 ;Month counter

;A while loop that executed as long the desired number of months have been counted

while (iNumCount lt iNumMonths) do

;If current month is different to previous month …

if (Num2Date(iStartDate, "M") ne Num2Date(iStartDate - 1, "M")) then

;…add 1 month to the counter

iNumCount = Sum(iNumCount + 1)

endif

;Add 1 day to the counter for the next iteration of the while loop

iStartDate = Sum(iStartDate, 1)

endwhile

;Show result in textfield etc.

Textfield = Num2Date(Sum(iStartDate, -1, iNumSubtractDays), cPattern)

endif

Add the script above into the exit event of you start date field and set the scripting language to FormCalc.