Script to calculate number of days between 2 dates

Avatar

Avatar
Ignite 3
Level 3
lostfrogg
Level 3

Likes

8 likes

Total Posts

66 posts

Correct reply

0 solutions
Top badges earned
Ignite 3
Ignite 1
Give Back 5
Give Back 3
Give Back 25
View profile

Avatar
Ignite 3
Level 3
lostfrogg
Level 3

Likes

8 likes

Total Posts

66 posts

Correct reply

0 solutions
Top badges earned
Ignite 3
Ignite 1
Give Back 5
Give Back 3
Give Back 25
View profile
lostfrogg
Level 3

26-08-2020

I'm fairly new to scripting and have looked for an answer to this; I have found some posts though have been unable to apply them to my scenario. 

I have one date that is automatically populated with the current date, and another (earlier) date that the user enters. I need to have a field which calculates the difference between these 2 dates in days.

Any help appreciated - I'll use any help as a learning exercise!

Accepted Solutions (0)

Answers (3)

Answers (3)

Avatar

Avatar
Give Back
Level 3
Kosta_Prokopiu
Level 3

Likes

15 likes

Total Posts

52 posts

Correct reply

9 solutions
Top badges earned
Give Back
Boost 5
Boost 3
Boost 10
Boost 1
View profile

Avatar
Give Back
Level 3
Kosta_Prokopiu
Level 3

Likes

15 likes

Total Posts

52 posts

Correct reply

9 solutions
Top badges earned
Give Back
Boost 5
Boost 3
Boost 10
Boost 1
View profile
Kosta_Prokopiu
Level 3

01-09-2020

Regarding the change of output format make the following changes:

Set the Data pattern of your 2 date fields to

Kosta_Prokopiu_0-1598950203772.png

and the Display pattern to the one you wish to have

Change the calculation script (if you use mine in FormCalc) to

Kosta_Prokopiu_1-1598950336495.png

What this does is to use the data format of your fields to get the date values for calculation which stays fixed whereas your display format can then change because we do not use the formattedValue properties any more.

 

Kosta_Prokopiu_2-1598950455770.png

 

Avatar

Avatar
Give Back
Level 3
Kosta_Prokopiu
Level 3

Likes

15 likes

Total Posts

52 posts

Correct reply

9 solutions
Top badges earned
Give Back
Boost 5
Boost 3
Boost 10
Boost 1
View profile

Avatar
Give Back
Level 3
Kosta_Prokopiu
Level 3

Likes

15 likes

Total Posts

52 posts

Correct reply

9 solutions
Top badges earned
Give Back
Boost 5
Boost 3
Boost 10
Boost 1
View profile
Kosta_Prokopiu
Level 3

26-08-2020

ok, then a slightly changed form

Kosta_Prokopiu_0-1598453764013.png

 

Datefield2 without pre-population in initialize

# of days calculated with slightly modified script

if (DateField1.rawValue ne null and DateField2.rawValue ne null)then
  Abs(Date2Num(DateField1.formattedValue, DateFmt(2)) - Date2Num(DateField2.formattedValue, DateFmt(2)))
else
  $.rawValue = ""
endif

Avatar

Avatar
Affirm 100
Employee
Kosta_Prokopiu1
Employee

Likes

165 likes

Total Posts

371 posts

Correct reply

107 solutions
Top badges earned
Affirm 100
Validate 1
Establish
Give Back 5
Give Back 3
View profile

Avatar
Affirm 100
Employee
Kosta_Prokopiu1
Employee

Likes

165 likes

Total Posts

371 posts

Correct reply

107 solutions
Top badges earned
Affirm 100
Validate 1
Establish
Give Back 5
Give Back 3
View profile
Kosta_Prokopiu1
Employee

26-08-2020

Here some links around this subject:

https://experienceleaguecommunities.adobe.com/t5/adobe-livecycle-questions/get-current-date/qaq-p/11...

https://experienceleaguecommunities.adobe.com/t5/adobe-livecycle-questions/using-formcalc-to-calcula...

 

Create a date field or entry

kprokopi_0-1598447977628.png

kprokopi_1-1598448007605.png

Then a calculated readonly date field

kprokopi_2-1598448072957.pngkprokopi_3-1598448105896.png

 

In the calculate event of the second field set FormCalc and place the following code:

if (DateField1.rawValue ne null)then
  Abs(Date2Num(DateField1.formattedValue, DateFmt(2)) - Date())
else
  $.rawValue = ""
endif

 

kprokopi_4-1598448218669.png

And the result is:

kprokopi_5-1598448259511.png