IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Finding difference between two dates to convert to hours


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
adarsh444

New User


Joined: 02 Nov 2006
Posts: 17

PostPosted: Thu Nov 02, 2006 11:24 pm
Reply with quote

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
View user's profile Send private message
David P

Active User


Joined: 11 Apr 2005
Posts: 106
Location: Cincinnati Ohio

PostPosted: Thu Nov 02, 2006 11:57 pm
Reply with quote

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
View user's profile Send private message
priyesh.agrawal

Senior Member


Joined: 28 Mar 2005
Posts: 1448
Location: Chicago, IL

PostPosted: Fri Nov 03, 2006 12:09 am
Reply with quote

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
View user's profile Send private message
adarsh444

New User


Joined: 02 Nov 2006
Posts: 17

PostPosted: Fri Nov 03, 2006 12:39 am
Reply with quote

THANKS A LOT
Back to top
View user's profile Send private message
adarsh444

New User


Joined: 02 Nov 2006
Posts: 17

PostPosted: Fri Nov 03, 2006 1:52 am
Reply with quote

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
View user's profile Send private message
priyesh.agrawal

Senior Member


Joined: 28 Mar 2005
Posts: 1448
Location: Chicago, IL

PostPosted: Fri Nov 03, 2006 2:11 am
Reply with quote

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
View user's profile Send private message
adarsh444

New User


Joined: 02 Nov 2006
Posts: 17

PostPosted: Fri Nov 03, 2006 2:22 am
Reply with quote

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
View user's profile Send private message
adarsh444

New User


Joined: 02 Nov 2006
Posts: 17

PostPosted: Fri Nov 03, 2006 2:42 am
Reply with quote

Just a correction.It's to be done using cobol functions and TIME1, TIME2
are not the table fields.
Back to top
View user's profile Send private message
priyesh.agrawal

Senior Member


Joined: 28 Mar 2005
Posts: 1448
Location: Chicago, IL

PostPosted: Mon Nov 06, 2006 2:50 am
Reply with quote

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
View user's profile Send private message
DavidatK

Active Member


Joined: 22 Nov 2005
Posts: 700
Location: Troy, Michigan USA

PostPosted: Mon Nov 06, 2006 10:25 pm
Reply with quote

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
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts VB to FB - Finding LRECL SYNCSORT 4
No new posts Need to convert date format DFSORT/ICETOOL 20
No new posts Keep leading zero(s) after convert fl... SYNCSORT 7
No new posts Timestamp difference and its average ... DB2 11
No new posts Convert single row multi cols to sing... DFSORT/ICETOOL 6
Search our Forums:

Back to Top