Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

Adding Days to a Date

Avatar

Level 1

Hello,

I have been trying to figure it out and even tried reading all the questions similar to this one so if anyone can help me better understand it would be greatly appreciated.

I have a field name "StartDate" and a field name "EndDate" I am trying to add Inspection dates.

StartDate + 30 days = 1st Inspection

StartDate + 60 days = 2nd Inspection (If EndDate has passed leave blank)

StartDate + 90 days = 3rd Inspection (If EndDate has passed leave blank)

StartDate + 120 days = 4th Inspection (If EndDate has passed leave blank)

StartDate + 150 days = 5th Inspection (If EndDate has passed leave blank)

StartDate + 180 days = 6th Inspection (If EndDate has passed leave blank)

EndDate + 14 days = Final Inspection

To anyone that can help thank you for your time.

AJ

1 Reply

Avatar

Former Community Member

I have the same basic need to manipulate dates, as have several other people.  Therefore, in helping you, I have helped myself.

When dealing with dates in LiveCycle, you have two scripting languages to choose from: JavaScript or FormCalc.  JavaScript will give you fine-grain manipulation--at the cost of more stringent programming; whereas FormCalc can give you the basics of what you are needing and easier-to-manipulate function calls.  This example uses FormCalc.

You already have your two date fields, StartDate and EndDate.  On each object, you can add the following to default those dates as today.  Put this line in the each of the respective "initialize" sections of the Script Editor window for your two date objects:

$.rawValue = Num2Date(Date(), DateFmt(2))

The Date() function grabs the current date on the system and returns a 1-up numerical value from the epoch date (1 Jan 1900).  The DateFmt(n) function sets the format; there are different formats to use, but be consistent throughout your form. The Num2Date(d, f) function converts a number into a date based on the epoch--for instance, "14" gives you "14 Jan 1900" and "42,748" gives you "14 Jan 2017"--and formats it as you want.

However, date fields in LiveCycle are formatted strings and you have to extract pieces of the data in order to add/subtract days.  Since it's easier to do math on a number than a string, create two variables StartDate_Num and EndDate_Num.

var StartDate_Num = Date2Num(StartDate.formattedValue, DateFmt(2))

var EndDate_Num = Date2Num(EndDate.formattedValue, DateFmt(2))

From there, it gets much easier.  The following presumes you have date fields for the following: FinalInspect_Date, Inspect1_Date, Inspect2_Date, Inspect3_Date, Inspect4_Date, Inspect5_Date, and Inspect6_Date.  Set your final inspection date and run through a couple of if-then-else scripts for each date-shift you need to achieve.  (BTW, you can subtract days as well...)  That's it.  Here's the script to add to your form:

FinalInspect_Date.rawValue = Num2Date(EndDate_Num + 14, DateFmt(2))

if((StartDate_Num + 30) < EndDate_Num) then

  Inspect1_Date.rawValue = Num2Date(StartDate_Num + 30, DateFmt(2))

else

  Inspect1_Date.rawValue = null

endif

if((StartDate_Num + 60) < EndDate_Num) then

  Inspect2_Date.rawValue = Num2Date(StartDate_Num + 60, DateFmt(2))

else

  Inspect2_Date.rawValue = null

endif

if((StartDate_Num + 90) < EndDate_Num) then

  Inspect3_Date.rawValue = Num2Date(StartDate_Num + 90, DateFmt(2))

else

  Inspect3_Date.rawValue = null

endif

if ((StartDate_Num + 120) < EndDate_Num) then

  Inspect4_Date.rawValue = Num2Date(StartDate_Num + 120, DateFmt(2))

else

  Inspect4_Date.rawValue = null

endif

if ((StartDate_Num + 150) < EndDate_Num) then

  Inspect5_Date.rawValue = Num2Date(StartDate_Num + 150, DateFmt(2))

else

  Inspect5_Date.rawValue = null

endif

if ((StartDate_Num + 180) < EndDate_Num) then

  Inspect6_Date.rawValue = Num2Date(StartDate_Num + 180, DateFmt(2))

else

  Inspect6_Date.rawValue = null

endif


Oh... And a word about .formattedValue versus .rawValue when it comes to dates -- use .rawValue when establishing a date value for the first time, but use .formattedValue otherwise.  A date field's .rawValue will be zero if the form user selects a date from the drop-down calendar rather than typing it in.  Use the .formattedValue for the Date2Num function to work properly as it requires a formatted date, not zero.