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
Here are some easy ways
EXEC SQL Set
ayOfWeekNum = DayOfWeek(:Checkdate) // Sunday is 1, Monday is 2, etc
EXEC SQL Set
ayOfWeekNum = DayOfWeek_ISO(:Checkdate) // Monday is 1, Tuesday is 2, etc
EXEC SQL Set
ayName = DAYNAME(:Checkdate);
Hi,
Take a look.
DayofWeek=%abs(%rem(%diff(tDate:d’2001-12-16′:*d):7));//0=Sunday
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…
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”
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
>> 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.
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.
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 ;
just use 0001-01-01 as the reference date, and then ALWAYS 0 = MON, 1 = TUE, … 6 = SUN
Hi Gray and FrmDeva
just fyi for non-free format (and SQLRPG length names)
C/EXEC SQL
OWNum = DayOfWeek(:Chkdte)
C+ Set
C/END-EXEC
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
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
then to use it just code
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