# Date Routine

**URL:** <https://techcommunity.softwareag.com/t/date-routine/76760>\
**Category:** Forum\
**Tags:** Adabas-Natural, Natural, Natural-Code-Samples\
**Created:** [May 20, 2008, 7:55pm UTC](https://techcommunity.softwareag.com/t/date-routine/76760 "2008-05-20T19:55:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Terence\_Lust](https://avatars.discourse-cdn.com/v4/letter/t/bb73d2/32.png) [@Terence\_Lust](https://techcommunity.softwareag.com/u/Terence_Lust)\
**Post date:** [May 20, 2008, 7:55pm UTC](https://techcommunity.softwareag.com/t/date-routine/76760/1 "2008-05-20T19:55:40Z")

</div>

Does anyone have a simple date routine to subtract hours from a full date time value (YYYYMMDDHHIISS).

Thanks for any help - I think I’m making this way too complicated!

Terry

---

<div class="post-metadata">

**Author:** ![Matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@Matthias1](https://techcommunity.softwareag.com/u/Matthias1)\
**Post date:** [May 20, 2008, 8:01pm UTC](https://techcommunity.softwareag.com/t/date-routine/76760/2 "2008-05-20T20:01:14Z")

</div>

If you use a time-field, it is very easy:

```auto
define data local
1 #time (T)
end-define
#time := *TIMX
write #time (EM=YYYY-MM-DD^HH:II:SS.T)
#time := #time - 2 * 60 * 60 * 10 /* subtract 2 hours
write #time (EM=YYYY-MM-DD^HH:II:SS.T)
end
```

---

<div class="post-metadata">

**Author:** ![Terence\_Lust](https://avatars.discourse-cdn.com/v4/letter/t/bb73d2/32.png) [@Terence\_Lust](https://techcommunity.softwareag.com/u/Terence_Lust)\
**Post date:** [May 20, 2008, 8:15pm UTC](https://techcommunity.softwareag.com/t/date-routine/76760/3 "2008-05-20T20:15:21Z")

</div>

Thanks Matthias!

---

<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:** [May 20, 2008, 8:17pm UTC](https://techcommunity.softwareag.com/t/date-routine/76760/4 "2008-05-20T20:17:15Z")

</div>

It’s easier than that. Forget all that multiplying by 60. Use a time constant (or initialized T-format variable) to add or subtract up to 24 hours, specified in hours, minutes and seconds.

```auto
DEFINE DATA LOCAL 
1 #T (T) INIT <TIMX> 
END-DEFINE 
SUBTRACT T'02:30:00' FROM #T /* 2 hrs 30 min 
DISPLAY *TIMX #T 
END
```

By the way, this type of question belongs in a “Natural Basics” thread. This thread is where you post code samples, open source, etc. 🙂

---

<div class="post-metadata">

**Author:** ![John\_Smith13](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@John\_Smith13](https://techcommunity.softwareag.com/u/John_Smith13)\
**Post date:** [April 27, 2015, 8:51pm UTC](https://techcommunity.softwareag.com/t/date-routine/76760/5 "2015-04-27T20:51:44Z")

</div>

If I want to calculate date and time, how can I do. For example, if I have as first date: 2015-01-01 at 23:40:43 and the second date 2015-01-05 at 03:50:10, how can I do the difference between them including date and time.If I use in separate fields, one day could be the difference depending on the time.  
I tried to use the format T for both date/time together, but for date, the value is strange: 0000-01-05. if hour2 is greater than hour1, the date is: 0000-01-06.  
Does anyone know how this 0000-01-05 can be translated in days.  
tks.

---

<div class="post-metadata">

**Author:** ![Jerome\_LeBlanc](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/jerome_leblanc/32/111_2.png) [@Jerome\_LeBlanc](https://techcommunity.softwareag.com/u/Jerome_LeBlanc)\
**Post date:** [April 27, 2015, 10:06pm UTC](https://techcommunity.softwareag.com/t/date-routine/76760/6 "2015-04-27T22:06:33Z")

</div>

Once you understand that a T field is just a number representing tenths of seconds from the time origin (1/1/0000 00:00:00.0) it is not hard to figure out.

```auto

DEFINE DATA LOCAL                                                
1 T1 (T) (EM=MM/DD/YYYY-HH:II:SS.T)                   
1 T2 (T) (EM=MM/DD/YYYY-HH:II:SS.T)                   
1 TDIFF (N12)                                                 
1 DAYS (N12)                                                 
1 HOURS (N12)                                                 
1 MINS (N12)                                                 
1 SECS (N12.1)                                               
1 TNTHS (N12.1)                                               
END-DEFINE                                                     
*                                                              
COMPUTE T1 = D'01/01/2015' + T'23:40:43'                       
COMPUTE T2 = D'01/05/2015' + T'03:50:10'                       
COMPUTE TDIFF = T2 - T1                                        
DISPLAY T1 T2 TDIFF                                          
DIVIDE 864000 INTO TDIFF GIVING DAYS REMAINDER TNTHS          
DIVIDE 36000 INTO TNTHS GIVING HOURS REMAINDER TNTHS          
DIVIDE 600 INTO TNTHS GIVING MINS REMAINDER TNTHS          
DIVIDE 10 INTO TNTHS GIVING SECS REMAINDER TNTHS          
WRITE / 1X '=' DAYS / '=' HOURS / 1X '=' MINS / 1X '=' SECS 
END                                                            
```

---

<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:** [April 27, 2015, 10:30pm UTC](https://techcommunity.softwareag.com/t/date-routine/76760/7 "2015-04-27T22:30:31Z")

</div>

John,

Take a look at the TMDIFF program in the code sample found here: [url][http://techcommunity.softwareag.com/ecosystem/communities/public/adanat/products/natural/codesamples/5ddd08f1-de89-11e4-89cb-cd8d7ef22065/?title=Date+%26+Time+Calculations[/url]](http://techcommunity.softwareag.com/ecosystem/communities/public/adanat/products/natural/codesamples/5ddd08f1-de89-11e4-89cb-cd8d7ef22065/?title=Date+%26+Time+Calculations%5B/url%5D)

---

<div class="post-metadata">

**Author:** ![John\_Smith13](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@John\_Smith13](https://techcommunity.softwareag.com/u/John_Smith13)\
**Post date:** [April 27, 2015, 11:38pm UTC](https://techcommunity.softwareag.com/t/date-routine/76760/8 "2015-04-27T23:38:40Z")

</div>

Thank you Jerome, thank you Ralph.

---

<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:49pm UTC](https://techcommunity.softwareag.com/t/date-routine/76760/9 "2021-04-02T14:49:28Z")

</div>


