View previous topic :: View next topic
|
Author |
Message |
adarsh444
New User
Joined: 02 Nov 2006 Posts: 17
|
|
|
|
hi everyone,
I want to calculate the difference between two dates in Cobol and convert it into hours.
below code is not giving me the correct result.
Code: |
WS-INT-DATE1 PIC 9(8) VALUE 20030924.
WS-INT-DATE2 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)
COMPUTE WS-HOURS = WS-DATE * 24
DISPLAY WS-DATE |
waiting for the reply. |
|
Back to top |
|
|
David P
Active User
Joined: 11 Apr 2005 Posts: 106 Location: Cincinnati Ohio
|
|
|
|
Hi,
As per your code
move ws-table-date to ws-int-date1
move ws-table-date to ws-int-date2
You are moving the same value in different fields before calculating their difference. (ws-table-date)
I believe you will always get zero as result....
Please confirm this once.
-thanks
David P. |
|
Back to top |
|
|
priyesh.agrawal
Senior Member
Joined: 28 Mar 2005 Posts: 1448 Location: Chicago, IL
|
|
|
|
Adarsh,
The actual problem is in this piece of the code you are using.
Code: |
COMPUTE WS-DATE =
FUNCTION DATE-OF-INTEGER(WS-NEW-DATE)
COMPUTE WS-HOURS = WS-DATE * 24 |
You dont need to convert it back to DATE in order to multiply it with 24. Just take a difference and multiply by 24.
Check the code below and let me know, if problems.
Code: |
WS-INT-DATE1 PIC 9(8) VALUE 20030924.
WS-INT-DATE2 PIC 9(8) VALUE 20020924.
WS-NEW-DATE PIC S9(9) COMP
WS-HOURS PIC 99999999.
*move ws-table-date to ws-int-date1 /* These are not necessary for testing as u already initialized values.
*move ws-table-date to ws-int-date2 /* These are not necessary for testing as u already initialized values.
COMPUTE TEMP-1 = FUNCTION INTEGER-OF-DATE(WS-INT-DATE1).
COMPUTE TEMP-2 = FUNCTION INTEGER-OF-DATE(WS-INT-DATE2).
COMPUT WS-NEW-DATE = TEMP1 - TEMP2.
COMPUTE WS-HOURS = WS-NEW-DATE * 24.
DISPLAY "FIRST DATE: " WS-INT-DATE1.
DISPLAY "SECOND DATE: " WS-INT-DATE2.
DISPLAY "DIFFERENCE-DAY: " WS-NEW-DATE.
DISPLAY "WS-HOURS :" WS-HOURS. |
|
|
Back to top |
|
|
adarsh444
New User
Joined: 02 Nov 2006 Posts: 17
|
|
|
|
THANKS A LOT |
|
Back to top |
|
|
adarsh444
New User
Joined: 02 Nov 2006 Posts: 17
|
|
|
|
I got the result its working fine.
If i want to calculate the difference between time in Cobol.
I cannot use FUNCTION DATE-OF-INTEGER(WS-INT-TIME).
Please suggest the efficient way to calculate the difference. |
|
Back to top |
|
|
priyesh.agrawal
Senior Member
Joined: 28 Mar 2005 Posts: 1448 Location: Chicago, IL
|
|
|
|
Quote: |
If i want to calculate the difference between time in Cobol.
I cannot use FUNCTION DATE-OF-INTEGER(WS-INT-TIME). |
First you'll have to understand the functioning of these COBOL Functions.
INTEGER-OF-DATE: returned value is an integer that is the number of days that the date represented by input date succeeds December 31, 1600 in the Gregorian calendar. The function result is a seven-digit integer with a range from 1 to 3,067,671.
DATE-OF-INTEGER: reversal of the function INTEGER-OF-DATE. The function result is an eight-digit integer. The returned value is an integer of the form YYYYMMDD.
Quote: |
Please suggest the efficient way to calculate the difference. |
Now if you want to calcualte time-difference, you may probably want to show some input format in which your time string comes. May be we can figure out something. |
|
Back to top |
|
|
adarsh444
New User
Joined: 02 Nov 2006 Posts: 17
|
|
|
|
I have
TIME1 VALUE HH.MM.SS.
TIME2 VALUE HH.MM.SS.
I want to calculate the time difference and
retrieve the hours.
COMPUTE WS-TIME-DIFF = TIME1 - TIME2
Does WS-TIME-DIFF(1:2) give the exact time difference in hours.
Could you suggest the efficient way to do this either using cobol or
sql- query?
Waiting for ur reply |
|
Back to top |
|
|
adarsh444
New User
Joined: 02 Nov 2006 Posts: 17
|
|
|
|
Just a correction.It's to be done using cobol functions and TIME1, TIME2
are not the table fields. |
|
Back to top |
|
|
priyesh.agrawal
Senior Member
Joined: 28 Mar 2005 Posts: 1448 Location: Chicago, IL
|
|
|
|
You could have better shown an input example with espected output.
Does time variable contain a 24 hr clock?
Quote: |
Does WS-TIME-DIFF(1:2) give the exact time difference in hours. |
Not necessarily... it depends what you expect from the output...
If Time-1 = 11:59:59 & Time-2 = 10:00:00... This difference would give you 1 hr. difference, but you might have been looking for a 2 instead.
So probably Evaluate/IFs can be used to round off the timestamp. |
|
Back to top |
|
|
DavidatK
Active Member
Joined: 22 Nov 2005 Posts: 700 Location: Troy, Michigan USA
|
|
|
|
adarsh444,
Code: |
01 FROM-TIME.
05 HH PIC 99.
05 FILLER PIC X.
05 MM PIC 99.
05 FILLER PIC X.
05 SS PIC 99.
01 TO-TIME.
05 HH PIC 99.
05 FILLER PIC X.
05 MM PIC 99.
05 FILLER PIC X.
05 SS PIC 99.
01 DAYS-DIFF PIC S9(3).
01 HOUR-DIFF PIC S9(5)V9(4).
01 DISPLAY-HOUR-DIFF PIC -----9.9999.
01 DISPLAY-DAYS-DIFF PIC +++9.
MOVE +1 TO DAYS-DIFF.
MOVE '16:42:16' TO FROM-TIME.
MOVE '22:10:30' TO TO-TIME.
COMPUTE HOUR-DIFF
= ((DAYS-DIFF * 86400)
+ (HH OF TO-TIME * 3600)
+ (MM OF TO-TIME * 60)
+ SS OF TO-TIME
- (HH OF FROM-TIME * 3600)
- (MM OF FROM-TIME * 60)
- SS OF FROM-TIME)
/ 3600.
MOVE HOUR-DIFF TO DISPLAY-HOUR-DIFF.
MOVE DAYS-DIFF TO DISPLAY-DAYS-DIFF.
DISPLAY 'THE NUMBER OF HOURS BETWEEN ' FROM-TIME
' AND ' TO-TIME ' ' DISPLAY-DAYS-DIFF ' DAYS'
' IS ' DISPLAY-HOUR-DIFF.
|
Results:
Code: |
.SARPAGE 4
.
.THE NUMBER OF HOURS BETWEEN 16:42:16 AND 22:10:30 +1 DAYS IS 29.4705
|
Round the result as you please.
Dave |
|
Back to top |
|
|
|