RPGLE pgm to determine the last day for a month

45 pts.
Tags:
RPG %DATE
RPG/400
RPGLE
does anyone have an rpgle pgm that will return the last day of the calendar month if given the month and year.

Answer Wiki

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

This may not be exactly what you’re looking for, but I think you can adapt it to your needs.

<pre>H datedit(*mdy) datfmt(*iso) debug timfmt(*hms)
H dftactgrp(*no) actgrp(*caller)
H expropts(*resdecpos)
H option(*srcstmt: *nodebugio)
H* ====================================================================
* Retrieve end of month, quarter, year
* ====================================================================

D Iso_p S 8
D Eow_p S 1
D Eom_p S 1
D Eoq_p S 1
D Eoy_p S 1

D Date S d

* ====================================================================
* Parameter lists
* ====================================================================

C *entry Plist
C parm iso_p
C parm eom_p
C parm eoq_p
C parm eoy_p

* ====================================================================
* Normal detail cycle
* ====================================================================

/Free
Eom_p = ’0′;
Eoq_p = ’0′;
Eoy_p = ’0′;

Test(de) *iso0 iso_p;
If %error;
date = %date();

Else;
date = %date(iso_p: *iso0);
EndIf;

If %subdt(date: *months) <> %subdt((date + %days(1)): *months);
Eom_p = ’1′;
If %subdt(date: *months) = 3 or
%subdt(date: *months) = 6 or
%subdt(date: *months) = 9 or
%subdt(date: *months) = 12;
Eoq_p = ’1′;
EndIf;
EndIf;

If %subdt(date: *years) <> %subdt((date + %days(1)): *years);
Eoy_p = ’1′;
EndIf;

*inlr = *on;
Return;
/End-free</pre>

————————————————————————————————————-
Try these
<pre>
h nomain option(*nodebugio:*srcstmt)

d PriorMonthEnd pr d
d pDate d const

d PriorMonthBegin…
d pr d
d pDate d const

d MonthBegin pr d
d pDate d const

d MonthEnd pr d
d pDate d const

d DayOfWeek pr 1p 0 Mon = 0 … Sun = 6
d pDate d const

d WeekDay pr n
d pDate d const

d WeekEnd pr n
d pDate d const

p PriorMonthEnd b
d PriorMonthEnd pi d
d pDate d const
/free
return pDate – %days(%subdt(pDate:*days));
/end-free
p PriorMonthEnd e

p PriorMonthBegin…
p b
d PriorMonthBegin…
d pi d
d pDate d const
/free
return MonthBegin(pDate – %months(1));
/end-free
p PriorMonthBegin…
p e

p MonthBegin b
d MonthBegin pi d
d pDate d const
/free
return PriorMonthEnd(pDate) + %days(1);
/end-free
p MonthBegin e

p MonthEnd b
d MonthEnd pi d
d pDate d const
/free
return PriorMonthEnd(pDate + %months(1));
/end-free
p MonthEnd e

p DayOfWeek b
d DayOfWeek pi 1p 0 Mon = 0 … Sun = 6
d pDate d const
/free
return %rem(%diff(pDate:d’0001-01-01′:*days):7);
/end-free
p DayOfWeek e

p WeekDay b
d WeekDay pi n
d pDate d const
/free
return %rem(%diff(pDate:d’0001-01-01′:*days):7) < 5;
/end-free
p WeekDay e

p WeekEnd b
d WeekEnd pi n
d pDate d const
/free
return %rem(%diff(pDate:d’0001-01-01′:*days):7) > 4;
/end-free
p WeekEnd e
</pre>

Discuss This Question: 5  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
  • GJN
    This example shows how to determine the month ending date for the current date. In order to determine the month ending date for any other date, substitute the desired date field or constant for %date. D @MthEnd S D DATFMT(*ISO) /free @MthEnd = %date - %days(%subdt(%date:*D)) + %days(1) + %Months(1) - %days(1); /end-free
    10 pointsBadges:
    report
  • Brazil Bob
    Use ADDUR to increment the month value and then set the date to the first of that month, then use SUBDUR to take away 1 day.
    10 pointsBadges:
    report
  • jrwwds
    Or just add one month and subtract the number of days in the given date... /free @MthEnd = %date + %Months(1) - %days(%subdt(%date:*D)); /end-free
    20 pointsBadges:
    report
  • jrwwds
    Oops... Sorry... My calcs won't work going from a given date with more days than the next month (ie: 1/30/09 is greater than 2/28/09)
    20 pointsBadges:
    report
  • Koohiisan
    Basically, let's say your current date is 3/15/2009, and you want to figure out the end of that month. You can create a timestamp value for the next month's FIRST date (4/1/2009), then subtract one day from that timestamp. It works flawlessly even when you have a leap year. So, if you have a field named CurDate with 20090315 (your date) in it, you could do something like this: D MonthEndTS S Z D CurDate S 8S 0 D MonthEnd S 8S 0 /free // create timestamp for first day of this month MonthEndTS = %timestamp(%subst(%char(CurDate):1:4) + '-' + %subst(%char(CurDate):5:2) + '-' + '01-00.00.00.000000'); // get last date of this month by adding one month and subtracting one day MonthEndTS = MonthEndTS + %months(1) - %days(1); // retrieve date portion to variable MonthEnd = %date(MonthEndTS); /end-free Please forgive any errors in that, as I kinda pieced it together in my head right now, from some past code I did. It could probably be cleaned up a tad bit, but it should do what you need.
    5,020 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