Calculating number of days from 2 given dates

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

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

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

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

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

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

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

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

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.

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.

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?

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.

You are moving to #T1 in both cases.

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

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.