360 pts.
 Day of Week in RPGLE
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

Software/Hardware used:
ASKED: May 4, 2009  1:26 PM
UPDATED: June 2, 2009  2:52 PM

Answer Wiki:
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!!!
Last Wiki Answer Submitted:  May 10, 2009  6:26 am  by  WilsonAlano   2,385 pts.
All Answer Wiki Contributors:  WilsonAlano   2,385 pts. , philpl1jb   44,060 pts. , BigKat   7,175 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

Here are some easy ways

EXEC SQL Set :D ayOfWeekNum = DayOfWeek(:Checkdate) // Sunday is 1, Monday is 2, etc

EXEC SQL Set :D ayOfWeekNum = DayOfWeek_ISO(:Checkdate) // Monday is 1, Tuesday is 2, etc

EXEC SQL Set :D ayName = DAYNAME(:Checkdate);

 3,115 pts.

 

Hi,

Take a look.

DayofWeek=%abs(%rem(%diff(tDate:d’2001-12-16′:*d):7));//0=Sunday

 2,385 pts.

 

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…

 360 pts.

 

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”

 7,175 pts.

 

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
 7,175 pts.

 

>> 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 pts.

 

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.

 5,525 pts.

 

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,275 pts.

 

just use 0001-01-01 as the reference date, and then ALWAYS 0 = MON, 1 = TUE, … 6 = SUN

 7,175 pts.

 

Hi Gray and FrmDeva

just fyi for non-free format (and SQLRPG length names)

C/EXEC SQL
C+ Set :D OWNum = DayOfWeek(:Chkdte)
C/END-EXEC

 7,175 pts.

 

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,385 pts.

 

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 pts.