Use RPG to find the day of the week 

 November 30, 2017

By  NickLitten

I wanted a handy little RPG code snippet to return the day name for a date. My requirement was to  feed in a date like “Nov 30 2017” and it to tell me that its ‘Thursday’. Google to the rescue, I found a nice little article by RAFAEL VICTORIA-PEREIRA (Thankyou Sir!) which has a code sample doing 99% of what I want. It’s a couple of years old so, a quick code refactor, modernize and here it is.

Original old style RPG article is here

Here are the two main code snippets in a Service program ready for your copy/paste delight:

Service Program @SRVDATE

 // +---------------------------------------------------------+
 // | Name - @SRVDATE                                         |
 // | Function - Look up a date and return the day name       |
 // +---------------------------------------------------------+
 // CRTRPGMOD MODULE(@srvdate )
 // BNDDIR('yourbinder')
 // ----------
 // CRTSRVPGM SRVPGM(@srvdate )
 // MODULE(@srvdate )
 // SRCMBR(@srvdate )
 // Modification History
 // 2017-11-30 NJL01 Created as part of version(185) revamp project.
 // ----------------------------------------------------------------
rpg day of week
 datfmt(*iso-) timfmt(*iso.)
 indent('| ') truncnbr(*yes) expropts(*resdecpos)
 copyright('| @srvdate 2017.11.30')

// Day of the week (returns the day of the week of a given date as nbr)
// From Monday(1) thru Sunday(7)
dcl-proc #rtvDayNumber export;
dcl-pi #rtvDayNumber zoned(1);
  p_Date date const;

dcl-s rtnDayNumber zoned(1) inz(9);
dcl-s RefDate date inz(d'0001-01-01');

 // Validate input date (this should be good but lets double check to be sure)
 TEST(E) p_Date;
   RETURN rtnDayNumber;

 // If the input parm is ok, calculate the day of the week
 rtnDayNumber = %REM(%DIFF(p_Date:RefDate:*DAYS):7)+1;

 return rtnDayNumber;

// Day of the week (returns the day of the week of a given date in text)
dcl-proc #rtvDayName export;
dcl-pi #rtvDayName char(10);
  p_Date date const;

dcl-s rtnDayName char(10) inz(*blanks);
dcl-s p_daynbr zoned(1);

 // calculate the day number for our input date
 p_daynbr = #rtvDayNumber(p_date);

 // if our date is squiffy then return an error
 IF p_DayNbr < 1 Or p_DayNbr > 7;
   rtnDayName = '*UNKNOWN';

   // If the input date is ok, Return the corresponding text
   when p_DayNbr = 1;
     rtnDayName = 'Monday';
   when p_DayNbr = 2;
     rtnDayName = 'Tuesday';
   when p_DayNbr = 3;
     rtnDayName = 'Wednesday';
   when p_DayNbr = 4;
     rtnDayName = 'Thursday';
   when p_DayNbr = 5;
     rtnDayName = 'Friday';
   when p_DayNbr = 6;
     rtnDayName = 'Saturday';
   when p_DayNbr = 7;
     rtnDayName = 'Sunday';

return rtnDayName;


Binder Language

 EXPORT SYMBOL(#rtvDayNumber)

Code Example

In our programs we would add that service program and then just do this for any given date:

// +-----------------------------------------------------------------+ 
// | #DATE - Date Handlign Procedures from SRVPGM (@SRVDATE) | 
// +-----------------------------------------------------------------+ 
dcl-pr #rtvDayNumber zoned(1); 
 *n date const; // p_date 
dcl-pr #rtvDayName char(10); 
 *n date const; // p_date 

then in our mainline code we simply ask it for the day name

name_of_day = @rtvDayName ( someDate );


Worked like a charm.

So, I’ve got the little service program working, documented it, written this blog in less time than I would have taken to figure it out and written it myself.

This internet thing is bloody useful sometimes. I think it’s going to catch on….



IBM i Software Developer, Digital Dad, AS400 Anarchist, RPG Modernizer, Shameless Trekkie, Belligerent Nerd, Englishman Abroad and Passionate Eater of Cheese and Biscuits. Nick Litten Dot Com is a mixture of blog posts that can be sometimes serious, frequently playful and probably down-right pointless all in the space of a day. Enjoy your stay, feel free to comment and remember: If at first you don't succeed then skydiving probably isn't a hobby you should look into.

Nick Litten

related posts:

  • Should really add the Date Format keyword to the Pr/Pi Nick or you may get “interesting” results if the programmer who uses this routine just happens to code a different default on the Ctl-Opt than was used when the service proc was created. Hard-coding th format along with Const deals with that.

    Also, although the logic I use is not exactly the same (a slightly bastardized version of mine appears in the RPG “Sorcerers” redbook) – if you do away with the +1 in the main calc and then add 7 if the result was < 1 then you don't need to handle a special case "unknown". In other words: WorkNum = %diff( WorkDate : Anysunday : *days); WorkDay = %rem(WorkNum :7); // Testing for < 1 allows for the situation where the input date // is earlier than the base date (AnySunday) If WorkDay < 1; Return WorkDay += 7; Else; Return WorkDay; Endif; In mine I use two lines to do the base calc because it was a teaching example developed when we first introduced subprocs back in V3r2.

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
    __CONFIG_colors_palette__{"active_palette":0,"config":{"colors":{"cff50":{"name":"Main Accent","parent":-1},"a344d":{"name":"Accent Transparent","parent":"cff50"}},"gradients":[]},"palettes":[{"name":"Default","value":{"colors":{"cff50":{"val":"var(--tcb-skin-color-0)"},"a344d":{"val":"rgba(46, 138, 229, 0.85)","hsl_parent_dependency":{"h":210,"l":0.54,"s":0.78}}},"gradients":[]},"original":{"colors":{"cff50":{"val":"rgb(0, 178, 255)","hsl":{"h":198,"s":1,"l":0.5}},"a344d":{"val":"rgba(0, 178, 255, 0.85)","hsl_parent_dependency":{"h":198,"s":1,"l":0.5}}},"gradients":[]}}]}__CONFIG_colors_palette__

    Get In Touch

    I’m always looking for awesome input, feedback and critique!