RPG ILE Day of Week

1245 pts.
Tags:
AS/400
RPG
RPGILE
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.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • JennyMack
    Hi Satsho, Can you please provide the programming language you're using? Thanks, Jenny Community Manager
    4,280 pointsBadges:
    report
  • Satsho
    Sorry, As/400, ILE RPG
    1,245 pointsBadges:
    report
  • Teandy
    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
  • Satsho
    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
  • graybeard52
    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
  • Satsho
    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
  • Splat
    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.
    6,835 pointsBadges:
    report
  • graybeard52
    >> 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
  • rpg4rico2
    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
  • TomLiotta
    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

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following