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

Addition and subtraction of dates in COBOL


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

Active User


Joined: 08 May 2008
Posts: 390
Location: China

PostPosted: Thu Mar 15, 2012 12:26 pm
Reply with quote

I wonder if COBOL support the function of addition and subtraction of dates.

for example, given a day, say 20120315, if I add 16 to this date, the result becomes 20120401 (April the 1st)

Help need on this. Thanks for your reply!
Back to top
View user's profile Send private message
krishna_ragav

New User


Joined: 29 Oct 2010
Posts: 10
Location: Chennai

PostPosted: Thu Mar 15, 2012 12:38 pm
Reply with quote

Hi,

As per my knowledge there are no statement which can calculate and produce proper date by COBOL.

Most of application would have separate date routine to calculate/ adjust the dates.


Regards,
Krishna
Back to top
View user's profile Send private message
Bill Woodger

Moderator Emeritus


Joined: 09 Mar 2011
Posts: 7309
Location: Inside the Matrix

PostPosted: Thu Mar 15, 2012 1:03 pm
Reply with quote

Have a look at the manuals for the list of Intrinsic Functions.

Language Environment also provides callable services.
Back to top
View user's profile Send private message
Peter cobolskolan

Active User


Joined: 06 Feb 2012
Posts: 104
Location: Sweden

PostPosted: Thu Mar 15, 2012 2:02 pm
Reply with quote

There is nothing than programming that solves the problem.
I recommend You read about COBOL Intrinsic Functions and date-functions.
Here is a small program example:
Code:

       Identification Division.
       Program-Id. DATENEXT.
       Data Division.

       Working-Storage Section.
       77 TodaysDate     Pic 9(08).
       77 IntTodaysDate  Pic 9(07).
       77 NextDate       Pic 9(08).
       77 IntNextDate    Pic 9(07).
       77 DayInterval    Pic 9(02) Value 15.

       Procedure Division.
           Move  Function CURRENT-DATE(1:8)  to TodaysDate
           Compute IntNextDate =
                   Function INTEGER-OF-DATE(TodaysDate) + DayInterval
           End-Compute

           Compute NextDate =
                   Function DATE-OF-INTEGER(IntNextDate)
           End-Compute

           Display 'The date ' DayInterval
                   ' days from ' TodaysDate
                   ' is ' NextDate
           GoBack
           .
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Thu Mar 15, 2012 2:50 pm
Reply with quote

If dejunzhu means direct manipulaiton of date-type variables in COBOL, then the answer is no, COBOL doesn't support them. If, however, dejunzhu just wants to manipulate dates, then the answer is yes, COBOL supports intrinsic functions that allow conversion of dates into integers, after which addition and subtraction is easy, as is using the intrinsic function to convert the integer back into a date.

Krishna, your answer is SEVERAL years out of date -- the intrinsic functions were added to COBOL quite a while back.
Back to top
View user's profile Send private message
krishna_ragav

New User


Joined: 29 Oct 2010
Posts: 10
Location: Chennai

PostPosted: Thu Mar 15, 2012 2:56 pm
Reply with quote

Thank You Robert.
Back to top
View user's profile Send private message
UmeySan

Active Member


Joined: 22 Aug 2006
Posts: 771
Location: Germany

PostPosted: Thu Mar 15, 2012 6:18 pm
Reply with quote

@ Peter cobolskolan

Morning Sir, Ursäkta mig !

Seams, that it does not work:

MOVE FUNCTION CURRENT-DATE(1:8) TO TODAYSDATE

COMPUTE INTNEXTDATE =
FUNCTION INTEGER-OF-DATE(TODAYSDATE) + DAYINTERVAL
END-COMPUTE

COMPUTE NEXTDATE =
FUNCTION DATE-OF-INTEGER(INTNEXTDATE)
END-COMPUTE

XPEDITER/TSO - SOURCE :

01 TODAYSDATE > 20120315
01 DAYINTERVAL > 03
01 NEXTDATE > 20120330 -> 15+03=18

Did i made something wrong, just following your instructions ???
Jag förstår inte.

Hjärtligt tack!
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Thu Mar 15, 2012 6:43 pm
Reply with quote

That's odd. My code is
Code:
01  WS-CURRENT-DATE.
    05  WS-CD                   PIC 9(08).
    05                          PIC X(13).
01  WS-INT-DATE                 PIC 9(07).
01  WS-DAYINTERVAL              PIC 9(07) VALUE 3.
01  WS-OUTPUT-DATE              PIC 9(08).

PROCEDURE DIVISION.
S1000-MAIN       SECTION.
    MOVE FUNCTION CURRENT-DATE  TO  WS-CURRENT-DATE.
    DISPLAY 'CURRENT DATE:  ' WS-CURRENT-DATE.
    COMPUTE WS-INT-DATE =
        FUNCTION INTEGER-OF-DATE (WS-CD) .
    DISPLAY 'INTEGER VALUE: ' WS-INT-DATE.
    ADD WS-DAYINTERVAL          TO  WS-INT-DATE.
    DISPLAY 'INTEGER VALUE: ' WS-INT-DATE.
    COMPUTE WS-OUTPUT-DATE =
        FUNCTION DATE-OF-INTEGER (WS-INT-DATE) .
    DISPLAY 'OUTPUT DATE:   ' WS-OUTPUT-DATE.
and it produces output of
Code:
 CURRENT DATE:  2012031509093382-0400
 INTEGER VALUE: 0150189
 INTEGER VALUE: 0150192
 OUTPUT DATE:   20120318
Back to top
View user's profile Send private message
UmeySan

Active Member


Joined: 22 Aug 2006
Posts: 771
Location: Germany

PostPosted: Thu Mar 15, 2012 7:03 pm
Reply with quote

@Peter cobolskolan
@Robert Sample

I just found out, that only playing with the interval-field during XPEDITER-session seams to be the problem. The first time, the calculation is done, everything's correct. Now, when i go back to re-run the calculation after chanching the value of the interval-field nothing is realy recalculated.

Only a new program-generation (compile/link) with a new value in the interval-field, will give a new correct result in the nextdate-field?

Holy smoke, seams to be very misterious. I wonder if the iluminati have a finger in the pie.
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 Replace each space in cobol string wi... COBOL Programming 2
No new posts COBOL -Linkage Section-Case Sensitive COBOL Programming 1
No new posts COBOL ZOS Web Enablement Toolkit HTTP... COBOL Programming 0
No new posts Calling DFSORT from Cobol, using OUTF... DFSORT/ICETOOL 5
No new posts Generate random number from range of ... COBOL Programming 3
Search our Forums:

Back to Top