Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating Day of Week from date... 1

Status
Not open for further replies.

RICHINMINN

Programmer
Dec 31, 2001
138
0
0
Does anyone know of a way to calculate the day of the week (Monday, Tuesday, etc.) from either a 6-digit (YYMMDD) or 8-digit (CCYYMMDD) date?
 
Hi Rich,

Here's a technique I've used. Maybe you can use it too.

Pick a Sunday date from sometime in the past (which one you choose will depend on the date ranges you're validating). Using cobol's intrinsic date function "INTEGER OF DATE", subtract the Sunday date from the subject date. Divide by 7; the remainder is the DOW relative to Sunday:

0 Sunday
1 Monday
2 Tuesday
etc.

HTH, Jack.
 
I had a similar situation a few years back , the way I got round it was to create a Cobol file

FD DAYSFILE
RECORD CONTAINS 8 CHARACTERS
LABEL RECORDS ARE STANDARD.
01 DAYSREC.
03 DAYSALT1.
05 DAYSDAYCODE PIC 9.
05 DAYSKEY.
07 DAYSDATE PIC 9(8) COMP-6.
03 FILLER PIC XXX.


What I did was I had a temporary program that created the file using a date in the past. Knowing the day of the week for this date I created a record and set the daycode to be a numeric value i.e. 1 - Sunday , 2 - Monday .....

I then added 1 to the day and set the following day of the week code to + 1 , I continued this process for several years (30 years) creating a record with the date and the relevant day of the week code (of course when the day code was 8 it was defaulted back to 1)

The end result is that I had a file that stored the day of the week permantly , and later programs that were developed could read this file to pick up the day of week.

This was done for commercial programs on a large machine with ample storage capacity and hence may not be viable for some.

The reply by Slade may be far more easily to implement , it was just that I found the reply interesting , even thou a few years too late for my requirements. What I have works for my circumstances and changing my programs would be a lot of work for no gain.

Cheers




 
Here's the code to implement the technique I mentioned earlier. To avoid text wrap-around I shortened data names, i.e. yr-date s/b your-date; daze s/b days-diff. Also, the DOW-TBL literal should begin in area B.

The usual caveats, disclaimers, and copouts apply.

Regards, Jack.

Code:
  05  YR-DATE           PIC  9(008) VALUE 20010104.
  05  ANY-SUN           PIC  9(008) VALUE 20001231.
  05  DAZE              PIC S9(008)       COMP.
  05  BIT-BUCKET        PIC S9(008)       COMP.
  05  DOW-SS            PIC S9(004)       COMP.
  05  DOW-TBL           VALUE
'MONDAY   TUESDAY  WEDNESDAYTHURSDAY FRIDAY   SATURDAY '.
      10  DOW-ENTRY OCCURS 6   PIC  X(009).





 COMPUTE DAZE  = FUNCTION INTEGER-OF-DATE(YR-DATE)
               - FUNCTION INTEGER-OF-DATE(ANY-SUN)
 DIVIDE  DAZE BY 7 GIVING BIT-BUCKET REMAINDER DOW-SS

 IF DOW-SS  = ZERO
    DISPLAY 'DAY OF WEEK FOR ' YR-DATE
            ' IS SUN'
 ELSE
    DISPLAY 'DAY OF WEEK FOR ' YR-DATE
            ' IS '  DOW-ENTRY(DOW-SS)(1:3)
 END-IF
 
It wrapped around anyway. Oh well.

Jack
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top