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 get first and last date of previous month

Former Member
0 Kudos

To set default values in selection screen,I want first and last date of the previous month.

Also I want the first and last dates of previous quarter.

The quarters can be only january to march, apr to june, jul to sept, oct to dec.

I want to run a program on beginning of each month by job scheduling having <b>selection screens</b> for the above monthly and quarterly dates.

9 REPLIES 9

Former Member
0 Kudos

You can use FM HR_JP_MONTH_BEGIN_END_DATE

You need to pass a particular date to this FM and this will return first and last day of that month.

Best Regards,

Vibha

<b>* Please mark all useful answers</b>

JozsefSzikszai
Active Contributor
0 Kudos

hi Suhas,

for the months you can try the following FMs.:

FIRST_DAY_IN_PERIOD_GET

LAST_DAY_IN_PERIOD_GET

About quarters I have no idea, but from the date you can determine the quarter, from the quarter the month and you can use the above FMs again.

hope this helps

ec

<b>* Please don't mark any unuseful answer</b>

andreas_mann3
Active Contributor
0 Kudos

1) prev. mth

concatenate sy-datum(4) sy-datum+4(2) '01' into hdate.

last = hdate - 1.

concatenate last(4) last+4(2) '01' into first

2)

quarter

clear I.

while I <> 1.

I = last mod( 3 ).

subtract 1 from last.

concatenate last(4) last(2) '01' into hdate.

qlast = hdate - 1.

concatenate last(4) last+4(2) '01' into qfirst

endwhile.

A.

Message was edited by:

Andreas Mann

former_member387317
Active Contributor
0 Kudos

subtract 1 months from current month value which you are getting from syst sturcture.

Try out below FM for last day of month..

RP_LAST_DAY_OF_MONTHS

and first day of any month will be 1st date only,,,

<b>Reward points if it is useful.</b>

Thanks & Regards

ilesh 24x7

Former Member
0 Kudos

HI Suhas,

following code will help you to get previous month last day , first day (No need to check leap year)...

Data: date like sy-datum.

Data: first_day like sy-datum.

data : last_day like sy-datum.

date = sy-datum.

date+6(2) = '01'.

first_day = last_day = date - 1.

first_day+6(2) = '01'.

andreas_mann3
Active Contributor
0 Kudos

Hi Suahs,

I think I've a solution for your Problem:

REPORT zcalcprevious.

PARAMETERS date TYPE sy-datum DEFAULT sy-datum.

DATA:
hdate TYPE sy-datum,
last  TYPE sy-datum,
qlast  TYPE sy-datum,
first TYPE sy-datum,
qfirst TYPE sy-datum,
d TYPE t5a4a-dlydy,
m TYPE t5a4a-dlymo,
y TYPE t5a4a-dlyyr.


*1) prev. mth
CONCATENATE date(4) date+4(2) '01' INTO hdate.
last = hdate - 1.
CONCATENATE last(4) last+4(2) '01' INTO first.
WRITE: / '1st', first, 'last', last.

*2) prev. quarter
m = last+4(2) mod 3 + 2.

CALL FUNCTION 'RP_CALC_DATE_IN_INTERVAL'
     EXPORTING
          date      = first
          days      = d
          months    = m
          signum    = '-'
          years     = y
     IMPORTING
          calc_date = qfirst.

WRITE: / 'q1st', qfirst.

CALL FUNCTION 'RP_CALC_DATE_IN_INTERVAL'
     EXPORTING
          date      = qfirst
          days      = d
          months    = 3
          signum    = '+'
          years     = y
     IMPORTING
          calc_date = qlast.


SUBTRACT 1 FROM qlast.
WRITE:  'qlast', qlast.

kind regards

Former Member
0 Kudos

<b>Hi SUHAS</b>

You must declare the type-pools first in order to use the types..

type-pools: tstr.

data : i_yeartab type tstr_yeartab,

i_ttstr type ttstr,

i_gensegtab type tstr_gensegyeartab.

call function 'TSTR_PERIODS_QUARTERS'

exporting

it_yeartab = i_yeartab

is_ttstr = i_ttstr

importing

et_gensegtab = i_gensegtab

exceptions

error = 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.

and for fist an last date of month u can use

You can use FM HR_JP_MONTH_BEGIN_END_DATE

You need to pass a particular date to this FM and this will return first and last day of that month.

Regards,

ABAP FRASHER..

Former Member
0 Kudos

Hi Suhas,

Use the below code.

DATA: v_date LIKE sy-datum.

DATA: v_month_begin_date TYPE sy-datum,

v_month_end_date TYPE sy-datum,

v_month(2) TYPE n,

v_month1(2) TYPE n,

v_quarter TYPE i,

v_year(4) TYPE n,

v_quarter_begda TYPE sy-datum,

v_quarter_endda TYPE sy-datum.

v_month = sy-datum+4(2).

IF v_month = '01'.

v_month = '12'.

v_year = sy-datum+0(4) - 1.

ELSE.

v_month = v_month - 1.

v_year = sy-datum+0(4).

ENDIF.

CONCATENATE v_year v_month '01' INTO v_date.

CALL FUNCTION 'HR_JP_MONTH_BEGIN_END_DATE'

EXPORTING

iv_date = v_date

IMPORTING

ev_month_begin_date = v_month_begin_date

ev_month_end_date = v_month_end_date.

v_month1 = sy-datum+4(2).

IF v_month1 = '01' OR

v_month1 = '02' OR

v_month1 = '03'.

v_quarter = 1.

ELSEIF v_month1 = '04' OR

v_month1 = '05' OR

v_month1 = '06'.

v_quarter = 2.

ELSEIF v_month1 = '07' OR

v_month1 = '08' OR

v_month1 = '09'.

v_quarter = 3.

ELSEIF v_month1 = '10' OR

v_month1 = '11' OR

v_month1 = '12'.

v_quarter = 4.

ENDIF.

IF v_quarter = 1.

v_quarter = 4.

v_year = sy-datum+0(4) - 1.

ELSE.

v_quarter = v_quarter - 1.

v_year = sy-datum+0(4).

ENDIF.

CALL FUNCTION 'HR_99S_GET_DATES_QUARTER'

EXPORTING

im_quarter = v_quarter

im_year = v_year

IMPORTING

ex_begda = v_quarter_begda

ex_endda = v_quarter_endda.

WRITE:/5 'previous month begin date : ', v_month_begin_date.

WRITE:/5 'previous month end date : ', v_month_end_date.

WRITE:/5 'previous quarter begin date : ', v_quarter_begda.

WRITE:/5 'previous quarter end date : ', v_quarter_endda.

Message was edited by:

Velangini Showry Maria Kumar Bandanadham

Former Member
0 Kudos

ok