## RPG ILE Day of Week

1245 pts.
Tags:
AS/400
RPG
RPG ILE
Are there alternate ways to determine the day of the week. I have the following formula - DayOfWeek = %Subst(('Sunday ' 'Monday ' 'Tuesday ' 'Wednesday' 'Thursday ' 'Friday ' 'Saturday ' ):(((%Rem(%Diff(%Date():%Date('1941-12-07'):*D):7)+1)*9))-8:9);

## Answer Wiki

Thanks. We'll let you know when a new response is added.

doesn’t look right – %SUBST divides one string and in this case it should be one long string 9 places per item. previous discussion got the day number

http://itknowledgeexchange.techtarget.com/itanswers/day-of-week-in-rpgle/

Recommend that you change the day from Pearl Harbor day to 0001-01-01 since it will now compute earlier dates without a computational error.

DayOfWeek = %Subst((‘Sunday Monday Tuesday WednesdayThursday Friday Saturday ‘ ):(((%Rem(%Diff(%Date():%Date(0001-01-01’):*D):7)+1)*9))-8:9);

Phil

————————————–
Please post your final code for the next guy/gal.

For anyone whose curious even with 0001-01-01 dates get screwed up when Britian and the British empire (except Scotland of course) adopted the current calander in 1751, to correct for the previous drift of Easter 1751 was a short year with only 282 days.
tks
Phil

## Discuss This Question: 10 Replies

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
• Hi Satsho, Can you please provide the programming language you're using? Thanks, Jenny Community Manager
4,280 pointsBadges:
report
• Sorry, As/400, ILE RPG
1,245 pointsBadges:
report
• I came up with this back in 2001 when I was learning to write sub procedures. It’s rather primitive but should be enough to get you started.
```*********************************************************************
*  PROGRAM NAME: DAYOFWEEK
*  CREATION DATE: 10/22/01
*  PURPOSE OF PROGRAM: CALCULATE THE CURRENT DAY OF THE WEEK
*  BY SUBTRACTING FROM A KNOWN SUNDAY I.E. JAN 2, 2000
*  RETURNS A NUMERIC VALUE AS FOLLOWS: SUN = 0 MON = 1 TUE = 2
*  WED = 3 THU = 4 FRI = 5 SAT = 6
*********************************************************************
HNOMAIN
HOPTION(*NODEBUGIO:*SRCSTMT)
*********************************************************************
* PROTOTYPE FOR PROCEDURE
*********************************************************************
D  DAYOFWEEKR     PR             1  0
D  DATE_CHAR                     8
*********************************************************************
* START PROCEDURE DEFINITION
*********************************************************************
P  DAYOFWEEKR     B                   EXPORT
D  DAYOFWEEKR     PI             1  0
D  DATE_CHAR                     8
*********************************************************************
*  DATE FIELDS
*********************************************************************
D DATE_WORK       S               D
D SUNDAY          S               D
*********************************************************************
*  FIELD DEFINITIONS
*********************************************************************
D WEEKS           S             15  0
D DAYOFWEEK       S              1  0
D ELAPSED         S             15  0
*********************************************************************
*  MOVE CURRENT DATE TO WORK FILED. GET A KNOWN SUNDAY
*********************************************************************
C     *USA          MOVEL     '01/02/2000'  SUNDAY
*********************************************************************
*  MOVE CURRENT CHARACTER DATE TO DATE DATA TYPE FIELD
*********************************************************************
C     *USA0         MOVE      DATE_CHAR     DATE_WORK
*********************************************************************
*  SUBTRACT CURRENT DATE FROM KNOWN SUNDAY
*********************************************************************
C     DATE_WORK     SUBDUR    SUNDAY        ELAPSED:*D
*********************************************************************
*  DIVIDE RESULT FIELD BY SEVEN. THE REMAINDER IS THE DAY OF
*  THE WEEK. IF THE REMAINDER IS LESS THAT 0, ADD SEVEN TO GET
*  THE CORRECT DAY.
*********************************************************************
C  ELAPSED       DIV       7             WEEKS
C                           MVR                     DAYOFWEEK
C                           IF        DAYOFWEEK < 0
C                          EVAL      DAYOFWEEK = DAYOFWEEK + 7
C                          ENDIF
*********************************************************************
*  RETURN DAY OF WEEK TO CALLING PROGRAM
*********************************************************************
C                       RETURN    DAYOFWEEK

P  DAYOFWEEKR     E
* * * *  E N D  O F  S O U R C E  * * * *
```
5,860 pointsBadges:
report
• Thanks, Teandy - but I was looking to use BIF's rather than actual RPG code. Philpl1jb - Thanks, will try your code. Appreciate your help. More ideas are welcome tho'.
1,245 pointsBadges:
report
• Seems like a lot of work when you can do it in one line of code. Use the BIF or SQL
`Set :DayName = DAYNAME(Checkdate);  `
Then you don't need all the rest of the code. BTW: The 1751/1752 change from Julian to Gregorian won't affect most of us, but I had to handle it when doing some ancestry stuff. They also changed the first day of the year from April 1 to January 1. Those who continued celebrating on April 1 were called "April Fools". The IBM calcs do not take this change into consideration - it handles all dates as Gregorian.
3,115 pointsBadges:
report
• Thanks Graybeard52 - unfortunately we are still in the process of moving to V6R1 and the DAYNAME function is not available in previous OS/400 versions.
1,245 pointsBadges:
report
• This is the code for a service program I've used for a while:
```H nomain

D Dayofweek       Pr             1p 0
D  inputdate                      d

D Dayname         Pr             9
D  inputdate                      d

* ====================================================================
* Dayofweek  - Calculate day of the week from input date
* ====================================================================
P Dayofweek       B                   Export

D Dayofweek       Pi             1p 0
D  workdate                       d

D Anysunday       S               d   inz(d'1582-10-17')
D Worknum         S              7  0
D Workday         S              1p 0

C     workdate      Subdur    anysunday     worknum:*d
C     worknum       Div       7             worknum
C                   Mvr                     workday
C                   If        workday <= *zero
C                   Return    workday + 7

C                   Else
C                   Return    workday
C                   EndIf

P Dayofweek       E

* ====================================================================
* Dayname    - Return name of week day from given date
* ====================================================================
P Dayname         B                   Export

D Dayname         Pi             9
D  workdate                       d

D Namedata        Ds
D  data                         63    inz('Monday   Tuesday  WednesdayThur+
D                                     sday Friday   Saturday Sunday')
D   name                         9    overlay(data:1) dim(7)

C                   Return    name(dayofweek(workdate))

P Dayname         E
```
I forget where I lifted the logic from, but it's worked rather well.
11,830 pointsBadges:
report
• >> unfortunately we are still in the process of moving to V6R1 and the DAYNAME function is not available in previous OS/400 versions << Sure it is. Back at least to v5R3. It's a SQL statement. SQL doesn't have to involve reading a file. I am at V5R4 and use it reguarly.
`EXEC SQL   Set :DayName = DAYNAME(Checkdate);  `
3,115 pointsBadges:
report
• If you didn't want to buy the SQL preprocessor, the following will work:    DayOfWeek =                                                            %Subst(('Sunday   Monday   Tuesday  WednesdayThursday Friday   Saturday '    ):(((%Rem(%Diff(%Date():%Date('1899-12-31'):                              *D):7)+1)*9))-8:9);                                                    Make sure you have 9 bytes (letters or blanks) for each day name.Rico
25 pointsBadges:
report
• I'm apparently missing something. The whole issue seems to be based on the %Date() that's buried in the middle. Since that returns current system date, it seems that the question is "What's the day-of-week today?" So just retrieve the QDAYOFWEEK system value. -- Tom
125,585 pointsBadges:
report