# Compute time/hours from date range

**URL:** <https://techcommunity.softwareag.com/t/compute-time-hours-from-date-range/94074>\
**Category:** Forum\
**Tags:** Adabas-Natural, Natural, Natural-on-Linux\
**Created:** [July 7, 2010, 3:29pm UTC](https://techcommunity.softwareag.com/t/compute-time-hours-from-date-range/94074 "2010-07-07T15:29:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Murray1](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@Murray1](https://techcommunity.softwareag.com/u/Murray1)\
**Post date:** [July 7, 2010, 3:29pm UTC](https://techcommunity.softwareag.com/t/compute-time-hours-from-date-range/94074/1 "2010-07-07T15:29:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/techcommunity/original/3X/c/d/cd19967bb605d28a72d77eb83a1cae22f3df2eb8.svg) [@system](https://techcommunity.softwareag.com/u/system)\
**Post date:** [July 7, 2010, 6:03pm UTC](https://techcommunity.softwareag.com/t/compute-time-hours-from-date-range/94074/2 "2010-07-07T18:03:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ralph\_Zbrog](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/ralph_zbrog/32/78_2.png) [@Ralph\_Zbrog](https://techcommunity.softwareag.com/u/Ralph_Zbrog)\
**Post date:** [July 7, 2010, 8:07pm UTC](https://techcommunity.softwareag.com/t/compute-time-hours-from-date-range/94074/3 "2010-07-07T20:07:37Z")

</div>

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.

```auto
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:

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

   2 06:10:01
```

---

<div class="post-metadata">

**Author:** ![Douglas\_Kelly](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/douglas_kelly/32/102_2.png) [@Douglas\_Kelly](https://techcommunity.softwareag.com/u/Douglas_Kelly)\
**Post date:** [July 7, 2010, 10:21pm UTC](https://techcommunity.softwareag.com/t/compute-time-hours-from-date-range/94074/4 "2010-07-07T22:21:46Z")

</div>

using

```auto
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.

---

<div class="post-metadata">

**Author:** ![Ralph\_Zbrog](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/ralph_zbrog/32/78_2.png) [@Ralph\_Zbrog](https://techcommunity.softwareag.com/u/Ralph_Zbrog)\
**Post date:** [July 9, 2010, 11:00am UTC](https://techcommunity.softwareag.com/t/compute-time-hours-from-date-range/94074/5 "2010-07-09T11:00:47Z")

</div>

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.

```auto
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:

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

       1 18:10:01
```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/techcommunity/original/3X/c/d/cd19967bb605d28a72d77eb83a1cae22f3df2eb8.svg) [@system](https://techcommunity.softwareag.com/u/system)\
**Post date:** [April 2, 2021, 2:58pm UTC](https://techcommunity.softwareag.com/t/compute-time-hours-from-date-range/94074/6 "2021-04-02T14:58:44Z")

</div>


