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

Calculate Hours between two dates with times;

morrisonmorgan
Registered: Jul 11 2010
Posts: 5

Hi!

Given a Start Date and an End Date, of the format MMM D, YYYY, HHHH - A Month, Day, Year, and a 4 digit 24 Hour Time, I then need to calculate the difference in Hours between the two dates?

So given dates like JUL 11, 2010, 1200 and July 12, 2010, 1400 it should return 26 as the # of hours elapsed.

I can calculate the numbre of full days, given this small FormCalc Script - but I think I'm in over my head with trying to figure the number of hours...

if (DateStart.rawValue ne null & DateEnd.rawValue ne null)then
(Abs(Date2Num(DateStart.formattedValue, DateFmt(2)) - Date2Num(DateEnd.formattedValue, DateFmt(2)))*24)
else
$.rawValue = ""
endif

My Product Information:
LiveCycle Designer, Windows
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
The Date2Num function returns the number of days not any smaller unit like a time value. You might look at the Time2Num function or using JavaScript's date time object.

George Kaiser

morrisonmorgan
Registered: Jul 11 2010
Posts: 5
Ok, So I'm using 4 input fields, StartDate / EndDate & StartTime / EndTime. (All times are calculated as military times) - I'm using LCD 8.0 + FormCalcCheck for non-null values, calculate hours run on first day + hours not run on last day, then nest an IF statement to check if I ran less than one day (2400 hours or less). I keep getting an error on my nested IF statement to check if the running time is >= 2400 hrs.? Code Below;if(DateStart.rawValue ne null & DateEnd.rawValue ne null & StartTime ne null & EndTime ne null)then//calculate Hours Run on first day, subtract from max possible of 2400
var HRS_START = 2400-StartTime

//calculate Hours NOT run on the last day of running.
var HRS_END = -2400+EndTime

//combined hours First+Last day.
var HR_CALC = HRS_START+HRS_END

//calculate number of days between a given date range and convert to Military Hours *100*24
var MAX_HRS = (Abs(Date2Num(DateStart.formattedValue, DateFmt(2)) - Date2Num(DateEnd.formattedValue, DateFmt(2)))*2400)

//check if I ran less than one day, in which case is the hours run, otherwise, add HR_CALC + MAX_HRS.
if (MAX_HRS >= 2400) then
MAX_HRS+HR_CALC
else
HR_CALC+2400

else
$.rawValue = ""
endif