These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

FormCalc Last Day of Month

chuckpatella
Registered: Aug 5 2009
Posts: 3

Hi everyone,

This is my first post to the forum. I am trying to calculate the last day of the month based on a Date field. The basic flow is the user selects a date (MM-DD-YYYY) from a date field (StartDate) and a related field (BillDate1) will automatically show the last day within the StartDate month.

I have searched everywhere for this type of coding and cannot find it. I'm new to FormCalc but have done some basic scripting in web languages. Any help would be greatly appreciated!

Thank you!

My Product Information:
LiveCycle Designer, Windows
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
You have to use the 'Date2Num()' and the "Num2Date()' funcitons to extract the month and from there you should be able to determine the the last day of the month. Remember February can have 28 or 29 days, so you will need to test for the leap year and leap century.

For the "BillDate1" field one could use the following calculation:
// process if date selectedif(HasValue(StartDate)) then// get the number of days since the Epoch datevar fDate = Date2Num(StartDate.formattedValue, "MM-DD-YYYY")// get the month from the number of days since the Epoch date as 2 digit stringvar fMonth = Num2Date(fDate, "MM")// get the full year as a 4 digit valuevar fFullYear =  Num2Date(fDate, "YYYY")// assume the month has 31 daysvar fEndDay = 31// test for months' that are less then 31 days longif(fMonth == 2) then// test for leap year - 2 digit year divisiable by 4 and // 4 digit year divisible by 400 or not divisible by 100 (a centry year)if((Mod(fFullYear, 4) == 0) AND ((Mod(fFullYear, 400) == 0) OR (Mod(fFullYear, 100) <> 0)) ) then// leap year/centuryfEndDay = 29else// not a leap yearfEndDay = 28endif // adjust for leap yearendif // end month == 2if(fMonth == 4) thenfEndDay = 30endifif(fMonth == 6) thenfEndDay = 30endifif(fMonth == 9) thenfEndDay = 30endifif(fMonth == 11) thenfEndDay = 30endif// build string if date selected// build the date string with the month, date and yearConcat(fMonth, "-", fEndDay, "-", fFullYear)else// no start date selectecnull // clear field endif // end has value for start date

George Kaiser

chuckpatella
Registered: Aug 5 2009
Posts: 3
Thank you! I transposed the code a little and came up with:

if(HasValue(StartDate)) then

var sd = StartDate;
var m;
var d;
var ld;
var y;

m=Date2Num(sd, "YYYY-MM-DD")
d=Num2Date(m, "DD")
y=Num2Date(m, "YYYY")
m=Num2Date(m, "MM")

if(m == 2) then
if ((Mod(y, 4) == 0) and ((Mod(y, 400) == 0) or (Mod(y, 100) <> 0)) ) then
29
else
28
endif
elseif (m==1 or m==3 or m==5 or m==7 or m==8 or m==10 or m==12) then
31
elseif (m==4 or m==6 or m==9 or m==11) then
30
endif
else
null
endif

Working great! Thank you for your help.