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

Cobol logic for Calculating difference between dates


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

New User


Joined: 18 Jan 2006
Posts: 16
Location: PUNE

PostPosted: Thu Mar 09, 2006 9:24 am
Reply with quote

hi every body

can any body help me for calculating the difference between two dates in the form of year month and date


thanks
mani
Back to top
View user's profile Send private message
priyesh.agrawal

Senior Member


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

PostPosted: Thu Mar 09, 2006 7:58 pm
Reply with quote

What is Input & how do you like to see output...

Regards,
Priyesh.
Back to top
View user's profile Send private message
martin9

Active User


Joined: 01 Mar 2006
Posts: 290
Location: Basel, Switzerland

PostPosted: Thu Mar 09, 2006 8:11 pm
Reply with quote

difference in what?
1. in days
2. in months
3. in years
possibly you need this in days,
then you must calculate your dates
into whole numbers first, ok.

unfortunately the date format is quite a bit wired...
just think of changing the number of days all 4 years
and not on all 400 years! (gregorian rule).

martin9
Back to top
View user's profile Send private message
DavidatK

Active Member


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

PostPosted: Fri Mar 10, 2006 1:55 am
Reply with quote

mani,

This routine will give you the number of days between two dates.

Code:

WORKING-STORAGE SECTION.                                       
                                                               
01  PROGRAM-VARIABLES.                                         
    05  WS-DATE-Y-M-D-1          PIC X(10).                     
    05  WS-DATE-Y-M-D-2          PIC X(10).                     
    05  WS-DATE-Y-M-D            PIC X(10).                     
    05  WS-DATE-YMD              PIC 9(8).                     
    05  WS-DAYS-TO-DATE          PIC 9(9).                     
    05  WS-DAYS-TO-DATE-1        PIC 9(9).                     
    05  WS-DAYS-TO-DATE-2        PIC 9(9).                     
    05  WS-DAY-IN-WEEK-NBR       PIC 9.                         
    05  WS-DAY-IN-WEEK-NBR-1     PIC 9.                         
    05  WS-DAY-IN-WEEK-NBR-2     PIC 9.                         
    05  WS-DAYS-DIFF             PIC -(5)9.                     
    05  WS-DAYS.                                               
      10  FILLER                 PIC X(36)   VALUE             
      'SUNDAY   MONDAY   TUESDAY  WEDNESDAY'.                   
      10  FILLER                 PIC X(27)   VALUE             
      'THURSDAY FRIDAY   SATURDAY '.                           
PROCEDURE DIVISION.                                                 
                                                                   
    MOVE '2006-01-01'           TO WS-DATE-Y-M-D-1.                 
    MOVE '2006-03-01'           TO WS-DATE-Y-M-D-2.                 
                                                                   
    MOVE WS-DATE-Y-M-D-1        TO WS-DATE-Y-M-D.                   
    PERFORM P2000-COMPUTE-DAYS-TO-DATE.                             
    MOVE WS-DAYS-TO-DATE        TO WS-DAYS-TO-DATE-1.               
    MOVE WS-DAY-IN-WEEK-NBR     TO WS-DAY-IN-WEEK-NBR-1.           
                                                                   
    MOVE WS-DATE-Y-M-D-2        TO WS-DATE-Y-M-D.                   
    PERFORM P2000-COMPUTE-DAYS-TO-DATE.                             
    MOVE WS-DAYS-TO-DATE        TO WS-DAYS-TO-DATE-2.               
    MOVE WS-DAY-IN-WEEK-NBR     TO WS-DAY-IN-WEEK-NBR-2.           
                                                                   
    COMPUTE WS-DAYS-DIFF = WS-DAYS-TO-DATE-2 - WS-DAYS-TO-DATE-1.   
                                                                   
    DISPLAY ' THERE ARE ' WS-DAYS-DIFF                             
            ' DAYS BETWEEN ' DAY-IN-WEEK (WS-DAY-IN-WEEK-NBR-1)     
            ' ' WS-DATE-Y-M-D-1                                     
            ' AND ' DAY-IN-WEEK (WS-DAY-IN-WEEK-NBR-2)           
            ' ' WS-DATE-Y-M-D-2.                                 
                                                                 
    GOBACK.                                                       
                                                                 
P2000-COMPUTE-DAYS-TO-DATE.                                       
    MOVE WS-DATE-Y-M-D(1:4)   TO WS-DATE-YMD(1:4).               
    MOVE WS-DATE-Y-M-D(6:2)   TO WS-DATE-YMD(5:2).               
    MOVE WS-DATE-Y-M-D(9:2)   TO WS-DATE-YMD(7:2).               
    COMPUTE WS-DAYS-TO-DATE                                       
            = FUNCTION INTEGER-OF-DATE (WS-DATE-YMD).             
    COMPUTE WS-DAY-IN-WEEK-NBR                                   
            = WS-DAYS-TO-DATE                                     
            - FUNCTION INTEGER (WS-DAYS-TO-DATE / 7) * 7 + 1.     

Code:

.SARPAGE 41                                                                   
.                                                                             
. THERE ARE     59 DAYS BETWEEN SUNDAY    2006-01-01 AND WEDNESDAY 2006-03-01


Dave
Back to top
View user's profile Send private message
mani_jnumca

New User


Joined: 18 Jan 2006
Posts: 16
Location: PUNE

PostPosted: Fri Mar 10, 2006 11:41 am
Reply with quote

i need difference between dates in the form of
yy/mm/dd format
if
i have input
date1----2006/01/01
date2----1956/02/22


then what is the difference between thease two dates in the form of
diffyy/diffmm/diffdd


thanks
mani
Back to top
View user's profile Send private message
Vidusha

New User


Joined: 01 Feb 2006
Posts: 20
Location: Chennai

PostPosted: Fri Mar 10, 2006 3:13 pm
Reply with quote

Hi Mani,

Here is a logic for doing that.

101 WS-DATE1 PIC X(10)
01 REDEFINES WS-DATE1
02 WS-DATE1-YYYY PIC 9(4)
02 FILLER PIC X(1)
02 WS-DATE1-MM PIC 9(2)
02 FILLER PIC X(1)
02 WS-DATE1-DD PIC 9(2)
*
01 WS-DATE2 PIC X(10)
01 REDEFINES WS-DATE2
02 WS-DATE2-YYYY PIC 9(4)
02 FILLER PIC X(1)
02 WS-DATE2-MM PIC 9(2)
02 FILLER PIC X(1)
02 WS-DATE2-DD PIC 9(2)

01 WS-DIFF PIC X(10)
01 REDEFINES WS-DIFF
02 WS-YYYY PIC 9(4)
02 FILLER PIC X(1)
02 WS-MM PIC 9(2)
02 FILLER PIC X(1)
02 WS-DD PIC 9(2)
*
SUBTRACT WS-DATE2-YYYY FROM WS-DATE1-YYYY GIVING WS-YYYY
IF WS-DATE1-MM < WS-DATE2-MM
SUBTRACT 1 FROM WS-YYYY
COMPUTE WS-MM = WS-DATE1-MM + 12 - WS-DATE2-MM
ELSE
SUBTRACT WS-DATE2-MM FROM WS-DATE1-MM GIVING WS-MM
END-IF
IF WS-DATE1-DD < WS-DATE2-DD
IF WS-DATE2-MM = 02
COMPUTE WS-DD = WS-DATE1-DD + 28 - WS-DATE2-DD
ELSE
IF WS-DATE2-MM = 01 OR 03 OR 05 OR 07 OR 08 OR 10 OR 12
COMPUTE WS-DD = WS-DATE1-DD + 31 - WS-DATE2-DD
ELSE
COMPUTE WS-DD = WS-DATE1-DD + 30 - WS-DATE2-DD
END-IF
END-IF
SUBTRACT 1 FROM WS-MM
ELSE
SUBTRACT WS-DATE2-DD FROM WS-DATE1-DD GIVING WS-DD
END-IF

(need to take care of Leap year as well)
If anything wrong, please correct and let me know

Thanks
Vidusha
Back to top
View user's profile Send private message
mani_jnumca

New User


Joined: 18 Jan 2006
Posts: 16
Location: PUNE

PostPosted: Fri Mar 10, 2006 3:31 pm
Reply with quote

thnks vidusha
i have checked it
its look fine
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 IDCAMS listcat service using cobol JCL & VSAM 1
No new posts Make cobol variable value a variable COBOL Programming 3
No new posts how to convert and use 1 bit hex-numb... COBOL Programming 10
No new posts VB file copy COBOL program gives erro... COBOL Programming 3
No new posts Cobol program to determine the sequen... COBOL Programming 2
Search our Forums:

Back to Top