Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

Formcalc: Calculate number of days between dates

Avatar

Level 3

Hi all,

I'm trying to set up a field that will show the number of days between a user-entered date and a current/today's date field. 

I tried to adapt a script from another post, but I'm not getting it to work.

form1.sf1.CCSub.CCGroup.#subform[0].DateField2::exit - (FormCalc, client)

form1.sf1.CCSub.CCGroup.#subform[0].NumericField2.rawValue = Date2Num(DateField1, "MM-DD-YY") - Date2Num(DateField2, "MM-DD-YY")

and here's my file:

https://www.dropbox.com/s/arkmnsxjkl4r156/AFBS_FacilitySpecialCareList02.pdf

any help would sure be appreciated!

Thanks!

Laura

3 Replies

Avatar

Level 10

Hi Laura,

I think the display patterns you are using are causing the problem.

Try, in the layout:ready event of DateField1 (though this would be better in the initialize event)

$.rawValue = Num2Date(Date(), "MM/DD/YYYY")

Then remove the code in the DateField1 exit event and add the following to the NumericField2 calculate event.

if (HasValue(DateField2)) then

Date2Num(DateField1, "MM/DD/YYYY") - Date2Num(DateField2, "YYYY-MM-DD")

endif

The data pattern for DateField2 of date{M/D/YY} seems to give a date in the YYYY-MM-DD format which is the default.  Might help to make it the same as DateField1 so you can stick with the same format in the code, if you can.

Bruce

Avatar

Level 3

Hi Bruce,

Thank you for the reply! I tried implementing your advice (I couldn't figure out how to change the date format for DateField1, thank you for that, it was driving me nuts), and I'm doing something wrong- I'm back to getting a result of 41,434 days between dates no matter what I use for the DateField2.  I was getting that number a couple of versions ago, as well: no matter what date I input, or what order I put the date calculation in, I got that same number.

It's sorta funny- I have no idea where that number is coming from. Can you tell from my file what I'm doing wrong?

https://www.dropbox.com/s/ohx9k5pv6a9xlw9/AFBS_FacilitySpecialCareList03.pdf

Thank you very much for your help.  As a non-programming design person, I'm totally stumped. I probably have some kind of logic breakdown  : ) 

Does it make sense, what I'm trying to make happen?  My boss wants to put a date into the "Date Listed" field (DateField2), and have the NumericField2 show how many days ago that was from the current date (DateField1)- the idea being that it would update whenever the form is opened, so the user can see how long a given client has been on the "Hot LIst"   I would probably make DateField2 a hidden field, once I get this script thing working.

Many thanks

Laura

Avatar

Level 10

Hi Laura,

There might be a bug with Reader here, the data format does not seem to work for an editable DateField.  I've changed the data format back to the default YYYY-MM-DD and it seems to work, does the internal format of the dates have to be MM/DD/YYYY? That is will the form be submitted to a server that expects this format?

The 41434 will be the number of days since 01 Jan 1990, as DateField2 wasn't formatting correctly the Date2Num function returned a value of zero.

If you don't need to display the current date then you can change the formula to

if (HasValue(DateField2)) then

    Date() - Date2Num(DateField2, "YYYY-MM-DD")

endif

Here is my version of your form https://workspaces.acrobat.com/?d=tn6H1ZQnKUjojbwkFrreDg

Bruce