# Compute time/hours from date range

Hi, does anybody have a smart and easy way, without using too many variables and manual coding, to calculate the time in hours, minutes and seconds between two date ranges.
Ex: Date1 = 2010-07-06 12:00:00.0 and
Date2 = 2010-07-07 12:00:00.0
Answer = 24:00:00:0 in hours, minutes etc.

Hi Murray,

A date variable (format=d) does not contain a time (12:00:00.0) but only a date.
Subtract the two dates from each other and multiply it by 24. Then you get no of hours. The rest will always be zero.

Use time (T) variables. If you canâ€™t guarantee that the difference will be less than 24 hours, then you also need date (D) variables to compute the number of days.

``````DEFINE DATA LOCAL
1 #TFROM (T)
1 #TTHRU (T)
1 #DFROM (D)
1 #DTHRU (D)
1 #DAYS (N3)
1 #HOURS (T)
END-DEFINE
ASSIGN #DFROM = #TFROM = E'07/05/2010 01:00:00'
ASSIGN #DTHRU = #TTHRU = E'07/07/2010 07:10:01'
ASSIGN #DAYS  = #DTHRU - #DFROM
ASSIGN #HOURS = #TTHRU - #TFROM
DISPLAY #DAYS
#HOURS (EM=HH:II:SS)
END``````

And the result:

``````#DAYS  #HOURS
----- --------

2  06:10:01``````

using

``````ASSIGN #DFROM = #TFROM = E'2010-07-05 07:00:00'
ASSIGN #DTHRU = #TTHRU = E'2010-07-07 01:10:01'``````

as input gives 2 days 18 hours as answer, but it is just 1 day 18 hours.

OK, Douglas, you caught me. I posted my code a little too quick, a little too dirty. Rather than a tried-and-true solution (usually what I post), this was an extrapolation of calculations I have done with dates, days, and time-zone differences.

The solution is 3 calculations instead of 4.

``````DEFINE DATA LOCAL
1 #TFROM (T)       INIT {E'07/08/2010 01:11:00'>
1 #TTHRU (T)       INIT <E'07/10/2010 01:10:59'}
1 #HOURS (T)
1 #D (D)
1 #DAYS (N7)
END-DEFINE
ASSIGN #HOURS = #TTHRU - #TFROM   /* duration
ASSIGN #D     = #HOURS            /* extract days
ASSIGN #DAYS  = #D                /* D to numeric format
DISPLAY #DAYS
#HOURS
END``````

And the result:

`````` #DAYS    #HOURS
-------- --------

1 18:10:01``````