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

Calculate days between two dates

affinit
Registered: Apr 7 2009
Posts: 18
Answered

Hi I'm trying to calculate the days between two dates on my form.

i've been reading through tutorials and most of them are add days onto days between dates.

http://www.bses.org.au/downloads/Steven_IT/travelworkout.pdf

this is my current form.

i've been working on getting drop down list populated by selecting A and gives you list in B .

then check B will give you a price in textfield C.

not sure if anyone can improve the code.

the last item is need is to calculate the difference in dates and then mulitply by C to get a total.

should i use formCal or use Javascript for this final function ?

i'm not sure how to code the calculate date.

we are trying to create a booking form for our staff for the hotels they may stay at.

kind regards

steven

My Product Information:
LiveCycle Designer, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
What do you mean by difference? This number of days? Hours?

Did you read the articles on dates and times?
http://www.acrobatusers.com/tutorials/2006/date_time_part2

This article has an example of of a date difference calculation that finds the number of days between two dates. The example is an AcroForm, but the technique is exactly the same on a LiveCycle form.

The first bit is to convert the Date strings in the text/date fields into JavaScript Date Objects, then find the difference with the "Date().getTime()" function. This will give you the difference in milliseconds. The last step is to convert the millisecond value into days, or whatever time unit you want.

You can also find gobs info on scripting with dates and times at www.pdfscripting.com.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
You can calculate the difference between two dates with FormCalc.

Example:
Put this script into the calculate event of a text field or numeric field.

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

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
First, your date fields should have a standard date format and then you can use either JavaScirpt of FormCalc to convert the date strings into a numeric value of that date from a standard Epoch date, sort of like the day of a century. There have been many post about this. With FormCalc you can use the "Date2Num" function to get the number of days from the Epoch date, the rest is then simple math.

George Kaiser

affinit
Registered: Apr 7 2009
Posts: 18
thanks for the help everyone. i did read through the tutorials as i am not an expert in Javascript i took some time to understand and it didn't work.

i also looked at the tutorial but when i opened up the pdf it said it wasn't created in livecycle so wasnt' sure how to adapt the code.

thank you all for your suggestions.

and thank you radzmar for your solution it worked like a charm.