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

FormCalc script for Date + calculation - very weird

poregon
Registered: Oct 27 2008
Posts: 80
Answered

I have a date field (DateStart) - I need a second field to calculate 18 months from that date, so here's the script I'm using

if(HasValue(DateStart)) then
Num2Date( (Date2Num(DateStart.formattedValue, "MMM DD, YYYY") + 548), "MMM DD, YYYY")
else
null
endif

This works fine if my DateStart is any day of the month EXCEPT the 1st thru the 9th. But if I choose a date from the 1st thru the 9th of any future month - then the date returned by this script goes funky - and returns July 02, 1901. Then returns to functioning normal from the 10th onward....until I get to the 1st thru the 9th of the next month.

Whaaaa?

poregon
Salix, Iowa

My Product Information:
LiveCycle Designer, Windows
poregon
Registered: Oct 27 2008
Posts: 80
I found the solution -

http://www.acrobatusers.com/forums/aucbb/viewtopic.php?id=20025

Thanks (again) Radzmar - but I still don't know what my problem was with my script - it's as if it recognizes no date if it's from the 1st through the 9th.

poregon
Salix, Iowa

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
Your conversion of the date string to the number of days from the Epoch Date for LiveCycle Designer, January 1, 1900, is failing and returning zero for those dates. 548 days after January 1, 1900 is July 2, 1901.

Have you looked at the intermediate results for each step of the calculation?

if(HasValue(DateStart)) thenvar sMsg = Concat("Start date: ", DateStart.formattedValue)var NumDays = Date2Num(DateStart.formattedValue, "MMM DD, YYYY")sMsg = Concat(sMsg, "\u000aDays since Epoch Date: ", Format("zzz,zz9", NumDays))var NumDays548 = NumDays + 548sMsg = Concat(sMsg, "\u000aDays in 548 days: ", Format("zzz,zz9", NumDays548))var EndDate = Num2Date(NumDays548, "MMM DD, YYYY")sMsg = Concat(sMsg, "\u000aEnd Date: ", EndDate)xfa.host.messageBox(sMsg, "Debugging", 3)Num2Date( (Date2Num(DateStart.formattedValue, "MMM DD, YYYY") + 548), "MMM DD, YYYY")elsenullendif

Are you sure 548 days can e used for computing an 18 month interval?

George Kaiser

poregon
Registered: Oct 27 2008
Posts: 80
Actually, no. Do you have a suggestion?

poregon
Salix, Iowa

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
You need to add the number of months to advance to the months on the date. This may require adjusting the years. Once one gets the adjusted date values one can build a date string and either display that or use it to format the result.

In FormCalc one code use:
if(HasValue(DateStart)) then// get date and parts of datevar fStart = Date2Num(DateStart.formattedValue, "MMM DD, YYYY")// get month as numbervar fMonth = Num2Date(fStart, "MM")// get date as numbervar fDate = Num2Date(fStart, "DD")// get 4 digit yearvar fYear = Num2Date(fStart, "YYYY")// advance monthsfMonth = fMonth + 18// adjust year for added monthsfYear = fYear + Floor(fMonth / 12)// adjust month between 1 and 12// get remainder of months divided by 12 months per yearfMonth = Mod(fMonth, 12)// make a remainder of 0 a value of 12if (fMonth == 0)thenfMonth = fMonth + 12endif// format month to 2 numbersfMonth = Format("99", fMonth)// create Iso Date stringvar sNewDate = Concat(fYear, "-", fMonth, "-", fDate)// display resultNum2Date(IsoDate2Num(sNewDate), "MMM DD, YYYY")elsenullendif

This is easier in JavaScirpt, as one does not need to adjust for larger than allowed days or months as the 'Date()' funciton will make the adjustments as needed.

George Kaiser

DaveyB
Registered: Dec 10 2010
Posts: 70
I'm sorry for the necro-post, but this particular problem helped me resolve a similar issue, and I thought it would be helpful to others to include the correct answer here!

poregon wrote:
...
if(HasValue(DateStart)) then
Num2Date( (Date2Num(DateStart.formattedValue, "MMM DD, YYYY") + 548), "MMM DD, YYYY")
else
null
endif


This works fine if my DateStart is any day of the month EXCEPT the 1st thru the 9th. But if I choose a date from the 1st thru the 9th of any future month - then the date returned by this script goes funky - and returns July 02, 1901. Then returns to functioning normal from the 10th onward....until I get to the 1st thru the 9th of the next month.

Whaaaa?
This stumped me for a while too, until I noticed where the error was:
The Date format used is "MMM DD, YYYY" while the default Date format is "MMM D, YYYY" (only one D). The script will execute perfectly on any date where the day has 2 digits, but on the first 9 days of the month where it is only single digit, that extra 'D' will cause an error.

Any error in the string will cause that part of the string to return null (zero), so adding the scripted 548 to the zero will result in the date "July 02, 1901" (548 days from epoch) for any single digit date.

Using a single D in the Date format will work properly for all the dates from 1 through 31, even though some of them are 2 digit.

Hope this helps someone else!

LiveCycle Designer 8.0
"Genius is one percent inspiration, ninety-nine percent perspiration." ~~ Thomas Edison
"If at first you don't succeed, get a bigger hammer." ~~ Alan Lewis
"If the conventional doesn't work, try the unconventional" ~~ DaveyB