Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in


Date Comparison in COBOL

Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
View previous topic :: :: View next topic  
Author Message

New User

Joined: 12 Apr 2007
Posts: 21
Location: Coimbatore

PostPosted: Mon Dec 24, 2007 10:28 pm    Post subject: Date Comparison in COBOL
Reply with quote

I have 2 date fields Date1 and Date2. They are both in MYY format i.e., a single byte month and 2 byte year. The month values are '1' '2' '3' '4' '5' '6' '7' '8' '9' '0' '-' '&' i.e., Month is alphanumeric and Oct, Nov and Dec are assigned with '0' '-' and '&' respectively.

I have to execute a paragraph if date1>date2.

The only logic that comes to my mind is regenerating 2 byte month from single byte month and assign century to year field. But this requires lot of variables in addition to the input file variables. Also lot of IF is used.

Can you help me in optimising the comparison?
Back to top
View user's profile Send private message


Active Member

Joined: 13 Jun 2007
Posts: 632
Location: Wisconsin

PostPosted: Tue Dec 25, 2007 8:18 am    Post subject:
Reply with quote

Whoever designed your input used poor data design.
Using one character for a month indicator is horrible when you have a known set of 12 and they gave a later month a sequence number lower than an earlier month. Only 2 bytes for a year because you can't tell when a century changes from say 2000 to 2100. If you aren't given the century then the best you are doing is guessing. If you have been guarenteed (not your own assumption) that the input years will always fall in the same century.


01 ws-input-date1
  05 ws-date1-m pic x.
    88 Dec1 value '&'.
    88 Nov1 value '-'.
    88 the rest of the months
  05 ws-date1-yy pic 9(2).
01 ws-input-date2
  05 ws-date2-m
    88 Dec2 value '&'.
    88 Nov2 value '-'.
    88 the rest of the months
  05 ws-date2-yy pic 9(2).

if (ws-date1-yy > ws-date2-yy)
   perform needed-paragraph
   if (ws-date1-yy = ws-date2-yy) AND (ws-date1-m NOT = ws-date2-m)
      if (Dec2 or Nov2 or Oct2) OR (Dec1 or Nov1 or Oct1)
         if dec1
            perform needed-paragraph
*here is where the messy part is, but I am too tired to write it for you now
*if you are any good, you should be able to come up with some date logic
* here yourself. it is not very easy because of poor data design and you
*don't want to reformat the date to something resonable that you can do
*numeric operations on. But then again it shouldn't be terribly difficult.
         if (ws-date1-m > ws-date2-m)
            perform needed-paragraph
end if
Back to top
View user's profile Send private message

Active User

Joined: 05 Dec 2006
Posts: 147
Location: Columbia, MO

PostPosted: Wed Dec 26, 2007 9:49 pm    Post subject: Reply to: Date Comparison in COBOL
Reply with quote

I work on a very archaic system where the dates are stored in MMDDYY on all flat and VSAM files, but have full DB2 dates on the tables.

So every program we have that uses any kind of date comparison has to use some sort of date conversion.

We have chosen to use date windowing as a stop-gap measure while the system is slowly converted (one file at a time) to using the CCYY-MM-DD format.

For date comparisons, we convert the dates to CCYYMMDD format. This involves code like this:


      03  WS-COMP-1-CC                 PIC 9(02).
      03  WS-COMP-1-YY                 PIC 9(02).
      03  WS-COMP-1-MM                 PIC 9(02).
      03  WS-COMP-2-DD                 PIC 9(02).

      03  WS-COMP-2-CC                 PIC 9(02).
      03  WS-COMP-2-YY                 PIC 9(02).
      03  WS-COMP-2-MM                 PIC 9(02).
      03  WS-COMP-2-DD                 PIC 9(02).

(in the copybook, the DB2 date is broken down by each portion of the date)



(do a similar move for the other date to WS-COMPARE-DATE-2 fields)

    do something
    do something else

Pathetic, but it works. Thank God we are a government agency and can get away with lousy performance.
Back to top
View user's profile Send private message

Active User

Joined: 24 May 2006
Posts: 258
Location: Salem, Oregon

PostPosted: Wed Dec 26, 2007 11:56 pm    Post subject:
Reply with quote

You can also use the date functions.

01 ws-date-area.
05 ws-compare-date-1 pic 9(08) value zeroes.
05 ws-compare-date-2 pic 9(08) value zeroes.

ws-input-date-1 and ws-input-date-2 are your YYMMDD input dates.
they account for the correct century or you can adjust the DATE WINDOW
to allow for years less than 50 years prior or 50 years greater then the
current year.

compute ws-compare-date-1 = FUNCTION DATE-TO-YYYYMMDD
compute ws-compare-date-2 = FUNCTION DATE-TO-YYYYMMDD

IF ws-compare-date-2 > ws-compare-date-1
Back to top
View user's profile Send private message
Warnings : 1

New User

Joined: 15 May 2006
Posts: 87

PostPosted: Fri Jan 11, 2008 4:45 pm    Post subject:
Reply with quote

If the date is in DDMMYY format .... then can it be converted to DDMMYYYY format ??

my question is like ... if I get the date 011020 ... would it be converted to 01101920 OR 01102020 ????

Any kind of help in this regard would be really appreciated.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming All times are GMT + 6 Hours
Page 1 of 1


Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Can sending 5 MB data between cobol p... Kevin Vaz CICS 11 Tue Oct 18, 2016 4:50 pm
This topic is locked: you cannot edit posts or make replies. Get correct date and time when curren... balaji81_k DB2 24 Fri Oct 14, 2016 10:40 pm
No new posts cobol embedded sql MS_Developer COBOL Programming 7 Fri Oct 14, 2016 2:39 pm
No new posts Cobol EZASOKET call to SETSOCKOPT fails Andi1982 COBOL Programming 6 Thu Oct 06, 2016 7:12 pm
No new posts COBOL 5 Compile oerdgie COBOL Programming 3 Thu Oct 06, 2016 6:14 pm

Back to Top
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us