Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to calculate last day of the month

Former Member
0 Kudos

Hi Gurus,

Please can anyone post a sample code to calculate last day of the month based on users input date.

For example:

User entry value: calender day = 20.05.2007

Then result should be = 31.05.2007

Thanks.

Any help greatly appreciated.

Best Regards,

Reddy.

8 REPLIES 8

Former Member
0 Kudos

use fm LAST_DAY_OF_MONTHS

REPORT ztestprg.

data : lv_lastday type sy-datum.

CALL FUNCTION 'LAST_DAY_OF_MONTHS'

EXPORTING

day_in = sy-datum

IMPORTING

LAST_DAY_OF_MONTH = lv_lastday

  • EXCEPTIONS

  • DAY_IN_NO_DATE = 1

  • OTHERS = 2

.

IF sy-subrc <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

write lv_lastday.

Former Member
0 Kudos

use <b>RP_LAST_DAY_OF_MONTHS</b>

REPORT ZKK_DATE.

SELECT-OPTIONS: S_DATE FOR SY-DATUM.

DATA: V_DATE1 LIKE SY-DATUM,

V_DATE2 LIKE SY-DATUM.

DATA: P_INTRANGE LIKE RSINTRANGE OCCURS 0 WITH HEADER LINE.

INITIALIZATION.

CALL FUNCTION 'RS_VARI_V_1_LAST_MONTH'

IMPORTING

P_DATE = V_DATE1

TABLES

P_INTRANGE = P_INTRANGE.

CALL FUNCTION 'RP_LAST_DAY_OF_MONTHS'

EXPORTING

DAY_IN = SY-DATUM

IMPORTING

LAST_DAY_OF_MONTH = V_DATE2

EXCEPTIONS

DAY_IN_NO_DATE = 1

OTHERS = 2.

IF SY-SUBRC <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

S_DATE-LOW = V_DATE1.

S_DATE-HIGH = V_DATE2.

APPEND S_DATE.

regards,

srinivas

0 Kudos

Hi SAPient,

I need to use this in already existing function module. I already know a function module which retirns last day of month based on users input value.

i.e. SLS_MISC_GET_LAST_DAY_OF_MONTH

I tried called calling above function module in another function module by call statement and import and export and chaning parameters, But when i executed the function module i am getting a error msg that raise exception occured.

I have't understood why i am getting that error. Thanks.

What is RP_LAST_DAY_OF_MONTHS? is it a subroutine/fm?

Best Regards,

Reddy.

0 Kudos

its a FM!

regards,

srinivas

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

The function module is the best way to go, but here is another way.



report  zrich_0001.

parameters: p_datum type sy-datum.

at selection-screen.

p_datum+4(2) = p_datum+4(2) + 1.
if p_datum+4(2) = '13'.
  p_datum+4(2) = '01'.
  p_datum(4) = p_datum(4) + 1.
endif.
p_datum+6(2) =  '01'.
p_datum = p_datum - 1.

Enter a date and hit enter.

Regards,

Rich Heilman

Former Member
0 Kudos

Hi Reddy,

Use the below FM:

CALL FUNCTION 'RP_LAST_DAY_OF_MONTHS'

EXPORTING

DAY_IN = sy-datum

IMPORTING

LAST_DAY_OF_MONTH = w_last_day.

Thanks and Best Regards,

Vikas Bittera.

**Reward if useful**

Former Member
0 Kudos

Hi,

You can use FM

SG_PS_GET_LAST_DAY_OF_MONTH

Thanks,

Sriram Ponna.

Former Member
0 Kudos

CALL FUNCTION 'HR_JP_MONTH_BEGIN_END_DATE'

EXPORTING

IV_DATE = <User_entry_date>

IMPORTING

EV_MONTH_BEGIN_DATE = FIRST_DATE

EV_MONTH_END_DATE = Last_date.