View previous topic :: View next topic |
Author |
Message |
adarsh444 Beginner
Joined: 19 Sep 2006 Posts: 13 Topics: 9
|
Posted: Thu Nov 02, 2006 11:15 am Post subject: Date difference |
|
|
I want to calculate the difference between two dates in Cobol.
below code is not giving me the correct result.
Code: |
ws-int-date1 PIC 9(8) value 20030924.
ws-int-date1 PIC 9(8) value 20020924.
WS-NEW-DATE PIC S9(9) COMP
move ws-table-date to ws-int-date1
move ws-table-date to ws-int-date2
COMPUTE WS-NEW-DATE =
FUNCTION INTEGER-OF-DATE(WS-INT-DATE1) -
FUNCTION INTEGER-OF-DATE(WS-INT-DATE2)
COMPUTE WS-DATE =
FUNCTION DATE-OF-INTEGER(WS-NEW-DATE)
DISPLAY WS-DATE
|
|
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Thu Nov 02, 2006 1:04 pm Post subject: |
|
|
adarsh444,
what do you mean wrong results? The date diff is 365 from the sample dates your provided.
see this code
Code: |
WORKING-STORAGE SECTION.
01 WS-INT-DATE1 PIC 9(8) VALUE 20030924.
01 WS-INT-DATE2 PIC 9(8) VALUE 20020924.
01 WS-DATE-DIFF PIC S9(09) COMP.
PROCEDURE DIVISION.
COMPUTE WS-DATE-DIFF =
FUNCTION INTEGER-OF-DATE(WS-INT-DATE1) -
FUNCTION INTEGER-OF-DATE(WS-INT-DATE2)
DISPLAY WS-DATE-DIFF
GOBACK.
|
Hope this helps...
Cheers
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
 |
dbzTHEdinosauer Supermod
Joined: 20 Oct 2006 Posts: 1411 Topics: 26 Location: germany
|
Posted: Thu Nov 02, 2006 1:37 pm Post subject: |
|
|
as pointed out in an other forum, try moving a different date to ws-int-date2. did you finally figure out the problem from a forum answer?
Quote: | move ws-table-date to ws-int-date1
move ws-table-date to ws-int-date2 |
_________________ Dick Brenholtz
American living in Varel, Germany |
|
Back to top |
|
 |
yadav2005 Intermediate

Joined: 10 Jan 2005 Posts: 348 Topics: 144
|
Posted: Fri Jul 17, 2009 3:37 pm Post subject: |
|
|
Kolusu,
I tried to use your code in my shop Code: |
COMPUTE WS-DATE-DIFF =
FUNCTION INTEGER-OF-DATE(WS-INT-DATE1) -
FUNCTION INTEGER-OF-DATE(WS-INT-DATE2)
|
But I am facing problems while compiling itself due to the COBOL version which we using , may be it is not supporting the feature , is there any other way to get the difference between dates. |
|
Back to top |
|
 |
tcurrier Intermediate
Joined: 10 Feb 2006 Posts: 188 Topics: 68
|
Posted: Fri Jul 17, 2009 4:48 pm Post subject: |
|
|
If your shop's compiler doesn't support the COBOL intrinsic date functions, then I would assume you would have to do it the old fashioned way. I found this code on another site, but I tried it and it works:
Code: | IDENTIFICATION DIVISION.
PROGRAM-ID. DATECALC.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 YR PIC 9999.
01 MON PIC 99.
01 DATE-1.
05 YR1 PIC 9999.
05 MON1 PIC 99.
05 D1 PIC 99.
01 DATE-2.
05 YR2 PIC 9999.
05 MON2 PIC 99.
05 D2 PIC 99.
01 QU1 PIC 99.
01 REM1 PIC 99.
01 REM2 PIC 99.
01 REM3 PIC 99.
01 T PIC 99999999 VALUE ZEROES.
01 T1 PIC 99999999.
01 T2 PIC 99999999.
PROCEDURE DIVISION.
MAINP.
MOVE 20030924 TO DATE-1. <-- your date #1
MOVE 20020924 TO DATE-2. <-- your date #2
PERFORM LP-PARA VARYING YR FROM 1900 BY 1 UNTIL YR = YR1.
PERFORM MN-PARA VARYING MON FROM 1 BY 1 UNTIL MON = MON1.
MOVE T TO T1.
COMPUTE T1 = T1 + D1.
MOVE 0 TO T.
PERFORM LP-PARA VARYING YR FROM 1900 BY 1 UNTIL YR = YR2.
PERFORM MN-PARA VARYING MON FROM 1 BY 1 UNTIL MON = MON2.
MOVE T TO T2.
COMPUTE T2 = T2 + D2.
COMPUTE T = T2 - T1.
DISPLAY 'THE DIFFERENCE BETWEEN DATES: '.
DISPLAY MON1'/'D1'/'YR1' & 'MON2'/'D2'/'YR2' IS:'.
DISPLAY T.
GOBACK.
LP-PARA.
DIVIDE YR BY 400 GIVING QU1 REMAINDER REM1.
DIVIDE YR BY 100 GIVING QU1 REMAINDER REM2.
DIVIDE YR BY 4 GIVING QU1 REMAINDER REM3.
IF REM1 = 0 OR ( REM2 IS NOT = 0 AND REM3 = 0 ) THEN
COMPUTE T = T + 366
ELSE
COMPUTE T = T + 365
END-IF.
MN-PARA.
DIVIDE YR BY 400 GIVING QU1 REMAINDER REM1.
DIVIDE YR BY 100 GIVING QU1 REMAINDER REM2.
DIVIDE YR BY 4 GIVING QU1 REMAINDER REM3.
IF MON = 1 OR MON = 3 OR MON = 5 OR MON = 7 OR MON = 8
OR MON = 10 OR MON = 12 THEN
COMPUTE T = T + 31
ELSE IF MON = 4 OR MON = 6 OR MON = 9 OR MON = 11 THEN
COMPUTE T = T + 30
ELSE IF REM1 = 0 OR ( REM2 IS NOT = 0 AND REM3 = 0 ) THEN
COMPUTE T = T + 29
ELSE
COMPUTE T = T + 28
END-IF. |
|
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Fri Jul 17, 2009 6:34 pm Post subject: |
|
|
tcurrier,
No offense but that is one heck of inefficient program to calculate the difference between 2 dates. Even if you have a cobol version that does NOT support COBOL intrinsic date functions, I wouldn't even touch that program.
1. Unnecessary calculations (lets say both both dates are 1 day apart(today and tomorrow as date1 and date2) you would spin calculating the days from 1900 year to 2009 and you would do the same again for date 2. why would you want to do that?
2. Fails for dates less than 1900-01-01
Please DO NOT POST solutions which you haven't coded
Kolusu |
|
Back to top |
|
 |
tcurrier Intermediate
Joined: 10 Feb 2006 Posts: 188 Topics: 68
|
Posted: Fri Jul 17, 2009 6:47 pm Post subject: |
|
|
Kolusu,
I understand.. No problem... But if I was in a pinch to come up with a date routine quickly (which I wouldn't be, since we have a vendor supplied date program), I'd at least use this temporarily until I came up with a more efficient routine ....
Sorry for posting someone else's code, though. |
|
Back to top |
|
 |
|
|