Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref

Author Message

New User

Joined: 02 Nov 2006
Posts: 17

Posted: Thu Nov 02, 2006 11:24 pm    Post subject: Finding difference between two dates to convert to hours

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

David P

Active User

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

 Posted: Thu Nov 02, 2006 11:57 pm    Post subject: Re: date difference cobol 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.
priyesh.agrawal

Senior Member

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

Posted: Fri Nov 03, 2006 12:09 am    Post subject: Re: Finding difference between two dates to convert to hours

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.

New User

Joined: 02 Nov 2006
Posts: 17

 Posted: Fri Nov 03, 2006 12:39 am    Post subject: THANKS A LOT

New User

Joined: 02 Nov 2006
Posts: 17

 Posted: Fri Nov 03, 2006 1:52 am    Post subject: 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.
priyesh.agrawal

Senior Member

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

Posted: Fri Nov 03, 2006 2:11 am    Post subject: Re: Finding difference between two dates to convert to hours

 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.

New User

Joined: 02 Nov 2006
Posts: 17

 Posted: Fri Nov 03, 2006 2:22 am    Post subject: 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

New User

Joined: 02 Nov 2006
Posts: 17

 Posted: Fri Nov 03, 2006 2:42 am    Post subject: Just a correction.It's to be done using cobol functions and TIME1, TIME2 are not the table fields.
priyesh.agrawal

Senior Member

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

Posted: Mon Nov 06, 2006 2:50 am    Post subject: Re: Finding difference between two dates to convert to hours

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

Active Member

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

Posted: Mon Nov 06, 2006 10:25 pm    Post subject: Re: Finding difference between two dates to convert to hours

 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
 All times are GMT + 6 Hours
 Page 1 of 1

Search our Forum:

 Topic Author Forum Replies Posted Similar Topics How to convert 2 bytes into single by... pkmurali DB2 3 Fri May 25, 2018 1:41 pm How can I convert charactor to fixed ... liying DFSORT/ICETOOL 7 Tue May 22, 2018 3:35 pm Difference between EQUAL and EQUAL TO... jithinraghavan COBOL Programming 3 Thu Apr 19, 2018 2:22 am ISMF Difference between volume count ... upendrasri IBM Tools 2 Tue Dec 05, 2017 12:40 pm Finding the plan when code compiled u... Ahemad DB2 2 Fri Dec 01, 2017 6:38 pm

 © 2003-2017 IBM MAINFRAME Software Support Division
 Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us