## Day of Week in RPGLE

365 pts.
Tags:
RPGLE
RPGLE Date Calculation
Hi, I need to get the DAY for the given date in RPGLE program Is there any efficient method available to calculate this or any reliable APIs? Heard that CEEDYWK is very useful API to calculate the DAY OF WEEK. if any of you have the sample code, please share with me. Thanks in advance. -Deva

## Answer Wiki

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

Here’s a sample of RPGLE source that gets Day-of-week with a division and remainder
http://archive.midrange.com/rpg400-l/200004/msg00715.html

This one packages the same logic it into a sub-procedure and returns day name.
http://209.85.173.132/search?q=cache:P9u-B_iVtAkJ:www.netshare400.com/cgi-bin/DSPSAMP%3Ffile%3DQRPGLESRC%26mbr%3DF.DATE+as/400+rpgle+day-of-week&cd=9&hl=en&ct=clnk&gl=us

and one more
http://midwareservices.com/RPGIV/internal_proc.htm

Phil

______________________________________________________________

if Given_date is NOT a date field
%rem(%diff(%date(Given_date):d’0001-01-01′:*days):7)

if Given_date is a date field
%rem(%diff(Given_date:d’0001-01-01′:*days):7)

both yield 0=Mon, 1=Tue, … , 6=Sun

Kevin

////////////////////////////

WilsonAlano

Why %abs of %rem ????
.. not that it’s going to give the wrong value,
just how can %rem ever return a negative number?

Phil

////////////////////////

Phil

The difference between both dates may be negative and %rem may results in negative number
“%REM returns the remainder that results from dividing operands n by m. The two operands must be numeric values with zero decimal positions. If either operand is a packed, zoned, or binary numeric value, the result is packed numeric. If either operand is an integer numeric value, the result is integer. Otherwise, the result is unsigned numeric. Float numeric operands are not allowed. <b>The result has the same sign as the dividend.</b>”

Wilson

///////////////////////

Kevin

To work, ‘0001-01-01’ must be Monday right?

Wilson

Hi all,
thanks for ur help!!! I have used the remainder logic and that works fine!!!

## Discuss This Question: 12 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.
• Here are some easy ways EXEC SQL Set :DayOfWeekNum = DayOfWeek(:Checkdate) // Sunday is 1, Monday is 2, etc EXEC SQL Set :DayOfWeekNum = DayOfWeek_ISO(:Checkdate) // Monday is 1, Tuesday is 2, etc EXEC SQL Set :DayName = DAYNAME(:Checkdate);
3,115 pointsBadges:
report
• Hi, Take a look. DayofWeek=%abs(%rem(%diff(tDate:d'2001-12-16':*d):7));//0=Sunday
2,710 pointsBadges:
report
• Hi Gray, Can we use the same line wht you have mentioned directly in my SQLRPG.? I will move the required date to Checkdate field. Please clarify me...
365 pointsBadges:
report
• Hi Wilson, Yes, 0001-01-01 is a Monday, and this eliminates the negative date difference, which I believe also messes up the day of week value. If you use a Sunday as reference, +6 days is a Saturday, but -1 days is a Saturday. The remainder of the difference divided by 7 is only valid for days GREATER than the reference. Which is one of the two main reasons I use 0001-01-01. (The other is that the weekend is >4) Kevin aka "BigKat"
9,090 pointsBadges:
report
• or I should say it is only valid for the same sign of the difference. That is dates greater than the the reference date always have one set of values, and dates less than the reference date always have another. assuming Sunday reference date
```d.o.w	>ref	<ref
SUN	0	-0
MON	1	-6
TUE	2	-5
WED	3	-4
THU	4	-3
FRI	5	-2
SAT	6	-1
```
9,090 pointsBadges:
report
• >> Can we use the same line wht you have mentioned directly in my SQLRPG.? <<< Frmdeva, Yes. Its intended for use in SQLRPGLE, just put a /free line ahead of it (and /end-free after) if you are using fixed form code. . I haven't checked obsolete versions of RPG, but it should work ok. The EXEC syntax might be a little different.
3,115 pointsBadges:
report
• Hi WilsonAlano, Very nice code example for calculating date of week. I really like that the code is complete in a single line. When I tired it with dates prior to 2001-12-16 it returned incorrect day numbers. Am I missing something in your code? As long as the date is later the 2001-12-16 all works just fine.
7,850 pointsBadges:
report
• WoodEngineer, I use a similar method to WilsonAlano's, and just adjust the result to account for an input date being prior to the reference date. I like using a formula where Sunday = 1 and Saturday = 7, so in the example below, I'm also adding 1. By adjusting the result, any known Sunday reference date will work. So if the days' difference between the two dates divided by 7 plus 1 is negative, then I add 7 and then use that result as an array index to look up the day names. (In this case, I don't want the absolute value, since I need to test the result). Example: Day = %Rem( %Diff( Date: d’2001-12-16′: *Days) : 7) + 1 ; IF Day < 1 ; Day += 7 ; ENDIF ;
4,290 pointsBadges:
report
• just use 0001-01-01 as the reference date, and then ALWAYS 0 = MON, 1 = TUE, ... 6 = SUN
9,090 pointsBadges:
report
• Hi Gray and FrmDeva just fyi for non-free format (and SQLRPG length names) C/EXEC SQL C+ Set :DOWNum = DayOfWeek(:Chkdte) C/END-EXEC
9,090 pointsBadges:
report
• Hi, To make work the same formula just change date '2001-12-06' to '0001-01-07'. It will combine BigKat technique with mine. Regards Wilson
2,710 pointsBadges:
report
• just as a note - in answer to your original question - yes the CEEDYWK API will do this set up the following prototype in your D specs
```D CEEDYWK         Pr                  OpDesc
D  LilianDate                   10I 0 Const
D  DayOfWeekNbr                 10i 0
D  FeedBack                     12A   Options(*Omit)
```
then to use it just code
```Ceedywk(LilianDate:DayOfWkN:*Omit);
```
the only drawback with this method is that it requires a lillian date for input - however you can use the CEEDAYS API to convert a date into lillian format if required
170 pointsBadges:
report