# Calculating number of days from 2 given dates

**URL:** <https://techcommunity.softwareag.com/t/calculating-number-of-days-from-2-given-dates/62304>\
**Category:** Forum\
**Tags:** Adabas-Natural, Natural, Natural-on-Mainframes\
**Created:** [June 13, 2007, 7:05pm UTC](https://techcommunity.softwareag.com/t/calculating-number-of-days-from-2-given-dates/62304 "2007-06-13T19:05:25Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![rishi](https://avatars.discourse-cdn.com/v4/letter/r/90db22/32.png) [@rishi](https://techcommunity.softwareag.com/u/rishi)\
**Post date:** [June 13, 2007, 7:05pm UTC](https://techcommunity.softwareag.com/t/calculating-number-of-days-from-2-given-dates/62304/1 "2007-06-13T19:05:25Z")

</div>

Hi

I need to calculate the number of days between two given dates

If my dates are 01/03/06 and 01/05/06 then I need to get the answer as 61

Tried subtracting the dates by moving them to a numeric field …  
But in vain…  
Can someone clarify me on this?  
Thanks

---

<div class="post-metadata">

**Author:** ![Steve\_Robinson](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/steve_robinson/32/107_2.png) [@Steve\_Robinson](https://techcommunity.softwareag.com/u/Steve_Robinson)\
**Post date:** [June 13, 2007, 7:24pm UTC](https://techcommunity.softwareag.com/t/calculating-number-of-days-from-2-given-dates/62304/2 "2007-06-13T19:24:57Z")

</div>

Date format, type D, is what you need. Use a MOVE EDITED to take what I presume are alpha strings with the date and convert them to date format; then subtract the date formatted variables. Here is the code:

DEFINE DATA LOCAL  
1 #DATE1-ALPHA (A6) INIT \<‘010306’\>  
1 #DATE2-ALPHA (A6) INIT \<‘010506’\>  
1 #DATE1-DATE (D)  
1 #DATE2-DATE (D)  
1 #DAYS (P5)  
END-DEFINE  
\*  
MOVE EDITED #DATE1-ALPHA TO #DATE1-DATE (EM=DDMMYY)  
MOVE EDITED #DATE2-ALPHA TO #DATE2-DATE (EM=DDMMYY)  
COMPUTE #DAYS = #DATE2-DATE - #DATE1-DATE  
WRITE ‘elapsed days’ #DAYS  
END

Page 1 07-06-13 06:23:11

elapsed days 61

One last point, if you do not know what date format is, demand a Natural class from management; or at least some documentation.

steve

---

<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:** [June 13, 2007, 9:53pm UTC](https://techcommunity.softwareag.com/t/calculating-number-of-days-from-2-given-dates/62304/3 "2007-06-13T21:53:31Z")

</div>

Vino,

At first I thought you were just confused, because there aren’t 61 days between Jan 3, 2006 and Jan 5, 2006. Then I realized I was confused (or perhaps egocentric) and your dates are in dd/mm/yy format. Given your input dates, it is unclear what the format is; it could have been yy/mm/dd for all I know. You would be better served by specifying the format, or at least using unambiguous dates for your examples, like 31/05/2006.

---

<div class="post-metadata">

**Author:** ![rishi](https://avatars.discourse-cdn.com/v4/letter/r/90db22/32.png) [@rishi](https://techcommunity.softwareag.com/u/rishi)\
**Post date:** [June 14, 2007, 2:24pm UTC](https://techcommunity.softwareag.com/t/calculating-number-of-days-from-2-given-dates/62304/4 "2007-06-14T14:24:37Z")

</div>

> [@Jerome LeBlanc](#):
>
> Vino,
> 
> At first I thought you were just confused, because there aren’t 61 days between Jan 3, 2006 and Jan 5, 2006. Then I realized I was confused (or perhaps egocentric) and your dates are in dd/mm/yy format. Given your input dates, it is unclear what the format is; it could have been yy/mm/dd for all I know. You would be better served by specifying the format, or at least using unambiguous dates for your examples, like 31/05/2006.

Next time I’ll be more specific…Sorry for the ambiguity

---

<div class="post-metadata">

**Author:** ![rishi](https://avatars.discourse-cdn.com/v4/letter/r/90db22/32.png) [@rishi](https://techcommunity.softwareag.com/u/rishi)\
**Post date:** [June 14, 2007, 2:33pm UTC](https://techcommunity.softwareag.com/t/calculating-number-of-days-from-2-given-dates/62304/5 "2007-06-14T14:33:30Z")

</div>

Thanks Steve… for a very quick reply…  
That helped me a lot…  
I was unaware that subtraction operation is allowed on type D fields.  
Thats where I missed …Thank you

---

<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:** [June 14, 2007, 3:16pm UTC](https://techcommunity.softwareag.com/t/calculating-number-of-days-from-2-given-dates/62304/6 "2007-06-14T15:16:58Z")

</div>

It even works with type T (Time) fields. There you get the time difference in tens of seconds.

```auto
define data local
1 #time1 (T) init <E'2007-06-14 06:00:00'>
1 #time2 (T) init <E'2007-06-14 07:00:00'>
1 #i4 (I4)
end-define
#i4 := #time2 - #time1
display #i4
end
```

---

<div class="post-metadata">

**Author:** ![Daniel\_Rose](https://avatars.discourse-cdn.com/v4/letter/d/4da419/32.png) [@Daniel\_Rose](https://techcommunity.softwareag.com/u/Daniel_Rose)\
**Post date:** [June 15, 2007, 10:58pm UTC](https://techcommunity.softwareag.com/t/calculating-number-of-days-from-2-given-dates/62304/7 "2007-06-15T22:58:15Z")

</div>

I tried that code you included and it did not work in the NATURAL that I am using. What release of NATURAL has this feature?

---

<div class="post-metadata">

**Author:** ![Steve\_Robinson](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/steve_robinson/32/107_2.png) [@Steve\_Robinson](https://techcommunity.softwareag.com/u/Steve_Robinson)\
**Post date:** [June 16, 2007, 1:50am UTC](https://techcommunity.softwareag.com/t/calculating-number-of-days-from-2-given-dates/62304/8 "2007-06-16T01:50:22Z")

</div>

> [@Daniel Rose](#):
>
> I tried that code you included and it did not work in the NATURAL that I am using. What release of NATURAL has this feature?

Which feature?

steve

---

<div class="post-metadata">

**Author:** ![Daniel\_Rose](https://avatars.discourse-cdn.com/v4/letter/d/4da419/32.png) [@Daniel\_Rose](https://techcommunity.softwareag.com/u/Daniel_Rose)\
**Post date:** [June 18, 2007, 10:57pm UTC](https://techcommunity.softwareag.com/t/calculating-number-of-days-from-2-given-dates/62304/9 "2007-06-18T22:57:33Z")

</div>

The INIT clause:

init \<E’2007-06-14 06:00:00’\>

I coded that clasue and I got errors when I tried to Check It.

dan

---

<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:** [June 19, 2007, 12:07am UTC](https://techcommunity.softwareag.com/t/calculating-number-of-days-from-2-given-dates/62304/10 "2007-06-19T00:07:29Z")

</div>

The format of date and time literals is determined by the DTFORM parameter. Your literal must match the defined sequence and delimiter character. To determine the format, run this one-line program.

```auto
DISPLAY *DATX .
```

See whether the format is yyyy-mm-dd or mm/dd/yy, etc. You’ll need to change the slashes to dashes, or move the year to the end, likely. The year will be 4 digits.

The E-format literal has been in Natural for a long time.

---

<div class="post-metadata">

**Author:** ![Daniel\_Rose](https://avatars.discourse-cdn.com/v4/letter/d/4da419/32.png) [@Daniel\_Rose](https://techcommunity.softwareag.com/u/Daniel_Rose)\
**Post date:** [June 20, 2007, 1:34am UTC](https://techcommunity.softwareag.com/t/calculating-number-of-days-from-2-given-dates/62304/11 "2007-06-20T01:34:19Z")

</div>

I coded a program just like you said. The result was “MM/DD/YY” format. I then coded a local variable as describe above:

1 #time1 (T) init \<E’2007-06-14 06:00:00’\>

I got an error. The cursor was positioned at the “\<” after the clause “INIT”. Here is the text of the error message:

NAT0094 Invalid initial value definition in DEFINE DATA statement.

So, based on this text, it would seem to me that data types of T can not be initialized in the Local Variable Definition area of the program. So, do I move a literal into the local variable, then?

---

<div class="post-metadata">

**Author:** ![Daniel\_Rose](https://avatars.discourse-cdn.com/v4/letter/d/4da419/32.png) [@Daniel\_Rose](https://techcommunity.softwareag.com/u/Daniel_Rose)\
**Post date:** [June 20, 2007, 1:40am UTC](https://techcommunity.softwareag.com/t/calculating-number-of-days-from-2-given-dates/62304/12 "2007-06-20T01:40:48Z")

</div>

I decided to go beyond that error. So, I created a program below:

DEFINE DATA  
LOCAL  
1 #T1 (T) /\* INIT \<E’06/19/07’\>  
1 #T2 (T) /\* INIT \<E’06/19/07’\>  
1 #D1 (N14) /\* INIT \<E’06/19/07’\>  
END-DEFINE  
MOVE EDITED ‘200706191236001’ TO #T1 (EM=YYYYMMDDHHIISST)  
MOVE EDITED ‘200706191336001’ TO #T1 (EM=YYYYMMDDHHIISST)  
#D1 := #T2 - #T1  
WRITE ‘=’ #D1  
END

When I ran this program, I got the following answer for #D1:

#D1: -633493929601

So, what am I doing wrong here? I was not expecting a negative number.

---

<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:** [June 20, 2007, 1:56am UTC](https://techcommunity.softwareag.com/t/calculating-number-of-days-from-2-given-dates/62304/13 "2007-06-20T01:56:10Z")

</div>

> [@](#):
>
> MOVE EDITED ‘200706191236001’ TO #T1 (EM=YYYYMMDDHHIISST)  
> MOVE EDITED ‘200706191336001’ TO #T1 (EM=YYYYMMDDHHIISST)  
> #D1 := #T2 - #T1

You are moving to #T1 in both cases.

---

<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:** [June 20, 2007, 2:51am UTC](https://techcommunity.softwareag.com/t/calculating-number-of-days-from-2-given-dates/62304/14 "2007-06-20T02:51:42Z")

</div>

All data types may be initialized at compile time, including Time variables.

If the result of the “DISPLAY \*DATX .” program was a date in the format mm/dd/yy, then the date portion of your time literals must be in that format, but with a 4-digit year, as in

```auto
1 #T (T) INIT<E'06/19/2007 13:10:00'>
```

Time literals always have both a date and time portion, with the time portion specified in hours:minutes:seconds.

---

<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:41pm UTC](https://techcommunity.softwareag.com/t/calculating-number-of-days-from-2-given-dates/62304/15 "2021-04-02T14:41:14Z")

</div>


