Here is an exmaple of adding days the script is used in the "exit" event for the date select field that has display format of "MM/DD/YYYY". Adding days requires add x number of days to the days since the epoch date for the current date, adding months or years one needs to manipulate the string parts of the date.
----- form1.#subform[0].InputDateField::exit: - (FormCalc, client) ---------------------------------
// fomatted string for selected date
var sFmtDateValue = $.formattedValue
var sMsg = Concat("Entered date formatted: ", sFmtDateValue) // build message string
sMsg = Concat(sMsg, "\u000a" ) // add new line to message
// convert date string to days since epoch date - format is important
var fDaysPast = Date2Num(sFmtDateValue, "MM/DD/YYYY")
// add 7 days to days past epoch date
var f7DaysPlus = fDaysPast + 7 // add 7 days
var s7DaysPlus = Num2Date(f7DaysPlus, "MMM DD, YYYY") // format string for 7 days plus
sMsg = Concat(sMsg, "\u000a", "Plus 7 Days: ", s7DaysPlus) // build message string
// add 14 days to days past epoch date
var f14DaysPlus = fDaysPast + 14 // add 7 days
var s14DaysPlus = Num2Date(f14DaysPlus, "MMMM DD, YYYY") // format string for 7 days plus
sMsg = Concat(sMsg, "\u000a", "Plus 14 Days: ", s14DaysPlus) // build message string
// display results
// work on months
// get parts of date past epoch date
var sFullYear = Num2Date(fDaysPast, "YYYY") // get 4 digit year form days past epoch date
var sMonth = Num2Date(fDaysPast, "MM") // get month form days past epoch date as number
var sDate = Num2Date(fDaysPast, "DD") // get date form days past epoch date as a number
var s2Month = Sum(sMonth, 2) // add 2 months
var s2FullYear = sFullYear
// if more than 12 months in new date adjust year on number of months
if (s2Month > "12") then
s2FullYear = Sum(s2FullYear, + 1) // increment year
s2Month = Sum(s2Month, - 12) // adjsut months
endif
var s2MonthsAdded = Concat(s2Month, "/", sDate, "/", s2FullYear) // date string
sMsg = Concat(sMsg, "\u000a", "Added 2 months: ", s2MonthsAdded) // display stringxfa.host.messageBox(sMsg, "Sample Adding Days" ,3, 0);
var s5Month = Sum(sMonth, 5) // add 5 months
var s5FullYear = sFullYear
// if more than 12 months in new date adjust year on number of months
if (s5Month > "12") then
s5FullYear = Sum(s5FullYear, + 1) // increment year
s5Month = Sum(s5Month, - 12) // adjsut months
endif
var s5MonthsAdded = Concat(s5Month, "/", sDate, "/", s5FullYear) //build Date string
sMsg = Concat(sMsg, "\u000a", "Added 5 months: ", s5MonthsAdded) // display stringxfa.host.messageBox(sMsg, "Sample Adding Days" ,3, 0);
// display results
xfa.host.messageBox(sMsg, "Sample Adding Days and Months" ,3, 0);