View previous topic :: View next topic |
Author |
Message |
hasan Beginner
Joined: 02 Sep 2004 Posts: 9 Topics: 3
|
Posted: Mon Sep 20, 2004 5:59 pm Post subject: DATE Arithmetic in COBOL..(any functions) |
|
|
Is there any way or functions in COBOL by which I can perform date arithmetic.
e.g. I want to subtract 3 YEARS from CURRENT-DATE.
Is there any function that does this. I checked in manuals, but couldn't get. Doing it through program will be complicated as I will have to validate things like leap year, days of month, etc.
Please suggest some solutions if any body knows. |
|
Back to top |
|
 |
rani Beginner
Joined: 21 Sep 2004 Posts: 2 Topics: 1
|
Posted: Tue Sep 21, 2004 1:25 am Post subject: |
|
|
If you want records for the last 10 days then change the sysin parm in step0100 to the following.
Code:
Code: |
SELECT CHAR(' INCLUDE COND=(8,8,ZD,EQ,')
,SUBSTR(CHAR(CURRENT DATE - 10 DAYS),1,4)
,SUBSTR(CHAR(CURRENT DATE - 10 DAYS),6,2)
,SUBSTR(CHAR(CURRENT DATE - 10 DAYS),9,2)
,CHAR(') ')
FROM SYSIBM.SYSDUMMY1
;
|
 |
|
Back to top |
|
 |
sandip Beginner
Joined: 28 Jul 2004 Posts: 18 Topics: 3
|
Posted: Tue Sep 21, 2004 4:14 am Post subject: |
|
|
I will use this code if you add particular no of days with a particular date -
Code: |
COMPUTE OUTPUT-DATE = FUNCTION DATE-OF-INTEGER(FUNCTION INTEGER-OF-DATE(WS-CURRENT-DATE) + INPUT-NO)
|
where, WS-CURRENT-DATE is in YYYYMMDD Format
INPUT-NO is 9(06).
Regards,
Sandip. |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Tue Sep 21, 2004 5:51 am Post subject: |
|
|
Hasan,
In simple terms , you can simply subtract 3 from the year postion of the date.
Code: |
01 WS-DATE.
05 W-CCYY PIC 9(4).
05 W-MM PIC 9(2).
05 W-DD PIC 9(2).
PROCEDURE DIVISION.
MOVE 20040921 TO WS-DATE
SUBTRACT 3 FROM W-CCYY
DISPLAY 'THE DATE AFTER SUBTRACTION IS :' WS-DATE
|
However you might get into trouble with leapyears. so if DB2 is an option then,
Code: |
EXEC SQL
SET : WS-PREV-DATE = DATE(:WS-CURR-DATE - 3 YEARS)
END-EXEC
|
You can add/subtract days from a given date. check this link for date manipulations
http://www.mvsforums.com/helpboards/viewtopic.php?t=2261
Hope this helps...
Cheers
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
 |
sandip Beginner
Joined: 28 Jul 2004 Posts: 18 Topics: 3
|
Posted: Tue Sep 21, 2004 5:57 am Post subject: |
|
|
Kolusu,
Simple solution with just one limitatation -
Your solution will not work for 29th. Feb of a leap year and we want to subtract some(not 4) years from it.
Regards,
Sandip. |
|
Back to top |
|
 |
hasan Beginner
Joined: 02 Sep 2004 Posts: 9 Topics: 3
|
Posted: Tue Sep 21, 2004 12:46 pm Post subject: |
|
|
Thanks for all solutions.
Its a pure COBOL program, no interface with DB2. I know that in DB2 its quite easy.
The problem I have is to check for leap years. I am using function INTEGER-OF-DATE and DATE-OF-INTEGER for subtraction. But as all you pointed out I will get into trouble for leap years.
Is there any other way in COBOL to check for leap years. Somebody told me that I can use LE callable services CEEDAYS. So if I pass leap year to it, one part of its feedback code will be zero. I checked in manuals, but nowhere it is said that CEEDAYS checks for leap year or indicates so by putting value in feedback code.
So is there any way to check for leap year in COBOL. |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Tue Sep 21, 2004 12:50 pm Post subject: |
|
|
Quote: |
So is there any way to check for leap year in COBOL.
|
hasan,
If you look at the link posted in my previous post, there is a solution to check for leap years
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
 |
hasan Beginner
Joined: 02 Sep 2004 Posts: 9 Topics: 3
|
Posted: Tue Sep 21, 2004 1:08 pm Post subject: |
|
|
Kolusu,
Sorry..I missed the links. I checked them after posting my reply. The solution suggested(by you) for leap year is simply excellent. Otherwise I can only think of all those if-else statements to check all those conditions for leap year making the program complicated and difficult to understand.
Thanks a lot. |
|
Back to top |
|
 |
sandip Beginner
Joined: 28 Jul 2004 Posts: 18 Topics: 3
|
Posted: Tue Sep 21, 2004 8:34 pm Post subject: |
|
|
Hasan,
INTEGER-OF-DATE and DATE-OF-INTEGER should take care of leap year automatically. Are you sure it is not working? Then I have to change our side one program also.
regards,
Sandip. |
|
Back to top |
|
 |
hasan Beginner
Joined: 02 Sep 2004 Posts: 9 Topics: 3
|
Posted: Wed Sep 22, 2004 1:10 pm Post subject: |
|
|
With INTEGER-OF-DATE you can add/subtract integer only, not years(or not months)
e.g. I can not say
Code: |
COMPUTE WS-RESULT =
INTEGER-OF-DATE(WS-CURRENT-DATE) - 3 YEARS
END-COMPUTE
|
to get it done, I will have to
Code: |
COMPUTE WS-RESULT = INTEGER-OF-DATE(WS-CURRENT-DATE) + 1095
END-COMPUTE
|
(where 1095 = 365 * 3 years)
Now I don't know whether any year of these 3 years was leap year or not. If it was a leap year, then I will have to subtract 1 from WS-RESULT.
So I used the following solution.
Code: |
INITIALIZE WS-CURRENT-DATE
MOVE FUNCTION CURRENT-DATE(1:8) TO WS-CURRENT-DATE
COMPUTE WS-INTEGER-OF-DATE =
FUNCTION INTEGER-OF-DATE(WS-CURRENT-DATE)
END-COMPUTE
MOVE 1 TO WC-INDEX
PERFORM UNTIL WC-INDEX > 3 (lets say, I want to subtract 3 years from CURRENT-DATE)
COMPUTE WS-RESULT-DATE-INT = WS-RESULT-DATE-INT - 365
END-COMPUTE
COMPUTE WS-DATE-OF-INTEGER =
FUNCTION DATE-OF-INTEGER(WS-RESULT-DATE-INT)
END-COMPUTE
*Now check whether the result-year is a leap year or not.(I used solution suggested by Kolusu)
MOVE WS-DATE-OF-INTEGER(1:4) TO WS-INPUT-NUM-YYYY
INITIALIZE WF-LEAP-YEAR
EVALUATE TRUE
WHEN FUNCTION MOD (WS-INPUT-NUM-YYYY 4) NOT ZERO
WHEN FUNCTION MOD (WS-INPUT-NUM-YYYY 100) ZERO
AND FUNCTION MOD (WS-INPUT-NUM-YYYY 400) NOT ZERO
SET NOT-LEAP-YEAR TO TRUE
WHEN OTHER
SET LEAP-YEAR TO TRUE
END-EVALUATE
IF LEAP-YEAR
COMPUTE WS-RESULT-DATE-INT = WS-RESULT-DATE-INT - WC-1
ELSE
CONTINUE
END-IF
ADD 1 TO WC-INDEX
END-PERFORM
COMPUTE WS-RESULT-DATE =
FUNCTION DATE-OF-INTEGER(WS-RESULT-DATE-INT)
END-COMPUTE
DISPLAY 'WS-RESULT-DATE : ' WS-RESULT-DATE
|
Please ignore the Formatting as it is not proper. |
|
Back to top |
|
 |
stingshi Beginner
Joined: 15 Apr 2004 Posts: 7 Topics: 2
|
Posted: Thu Sep 23, 2004 1:59 am Post subject: |
|
|
Hasan,
Now I have read that how to compute on date variables and how to check leap year.
But I am confused on your original question at last: what do you mean on the 'YEAR'?
Does it means (1) a fixed 365 days, or (2) 366 days for leap year, 365 days for others?
I guest you mean the second.
Then, for an example:
2004(leap)----2003----2002----2001----2000(leap)----1999
eg:
WS-CURRENT-DATE = 20040228
WS-YEAR = 5
then: WS-RESULT-DATE = 20040228 - (365 * 4) - 366 = 19990228
(as you showed above)
but as below, which result of WS-RESULT-DATE is your wanted?
(A)
WS-CURRENT-DATE = 20040228
WS-YEAR = 1
then: WS-RESULT-DATE = 20040228 - 365 = 20030228
(B)
WS-CURRENT-DATE = 20040229
WS-YEAR = 1
then: WS-RESULT-DATE = 20040228 - 365 = 20030301
(C)
WS-CURRENT-DATE = 20040229
WS-YEAR = 1
then: WS-RESULT-DATE = 20040228 - 366 = 20030228
(A)
WS-CURRENT-DATE = 20040228
WS-YEAR = 1
then: WS-RESULT-DATE = 20040228 - 366 = 20030227
I cant find your judgement above.
sting. |
|
Back to top |
|
 |
|
|