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.
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.
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
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?
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.