07-14-2020 10:08 AM
When I enter a date, it has to be validated and the following should be printed.
1. First and the last date of the current month of the entered date.
2. First and the last date of the previous month of the entered date.
3. First and the last date of the next month of the entered date.
I am not asking for the entire program but just a clue as to how to proceed. I am a beginner and yet to learn a lot. Any help would be appreciated.
Regards,
Srinath.
07-14-2020 12:04 PM
No FMs necessary, just simple ABAP based on TYPE d or datum (abap type date) using character replacement as well as addition/subtraction:
*&---------------------------------------------------------------------*
*& Report ZDEMO_DATE_FIRST_LAST_MONTH
*&---------------------------------------------------------------------*
REPORT zdemo_date_first_last_month.
PARAMETERS p_date TYPE d DEFAULT sy-datum. " the entered date
DATA tempdate TYPE d. " for calculation purposes
**********************************************************************
* 1) First and last of current month based on selected date
**********************************************************************
DATA firstofcurrmonth TYPE d. " the first of current month based on selected date
DATA lastofcurrmonth TYPE d. " the last of current month based on selected date
" 1.a) First of current month based on selected date
firstofcurrmonth = p_date(6) && '01'. " We know each month starts on the 1st, this one is easy
" 1.b) Last of current month based on selected date
tempdate = p_date(6) && '28'. " we know each month has at least 28 days
tempdate = tempdate + 4. " adding 4 days will bring us always into the next month
tempdate = tempdate(6) && '01'. " this will give us the first of the next month
lastofcurrmonth = tempdate - 1. " subtracting one, will give us the last of current month
**********************************************************************
* 2) First and last of previous month based on selected date
**********************************************************************
DATA firstofprevmonth TYPE d. " the first of previous month based on selected date
DATA lastofprevmonth TYPE d. " the last of previous month based on selected date
" 2.a) Last of previous month based on selected date
" We already know the first of the current month, so simply subtract one
lastofprevmonth = firstofcurrmonth - 1.
" 2.b) First of previous month based on selected date
firstofprevmonth = lastofprevmonth(6) && '01'.
**********************************************************************
* 3) First and last of next month based on selected date
**********************************************************************
DATA firstofnextmonth TYPE d. " the first of next month based on selected date
DATA lastofnextmonth TYPE d. " the last of next month based on selected date
" 3.a) First of next month based on selected date
" We could have grabbed this date already from step 1.b)
firstofnextmonth = lastofcurrmonth + 1. " But we can also simply add 1 to the last of the current month
" 3.b) Last of next month based on selected date
tempdate = firstofnextmonth(6) && '28'. " same logic as in step 1.b)
tempdate = tempdate + 4. " adding 4 days will bring us always into the next month
tempdate = tempdate(6) && '01'. " this will give us the first of the next, next month
lastofnextmonth = tempdate - 1. " subtracting one, will give us the last of next month
**********************************************************************
* Display dates
**********************************************************************
cl_demo_output=>write( p_date ).
cl_demo_output=>write( firstofcurrmonth ).
cl_demo_output=>write( lastofcurrmonth ).
cl_demo_output=>write( firstofprevmonth ).
cl_demo_output=>write( lastofprevmonth ).
cl_demo_output=>write( firstofnextmonth ).
cl_demo_output=>write( lastofnextmonth ).
cl_demo_output=>display( ).
07-14-2020 11:16 AM
Hi smummaka,
That question was asked and answered so many times thus I would suggest using Google before posting question.
How can i get statrt date of month to end date of month.
Regards,
Bartosz
07-14-2020 12:04 PM
No FMs necessary, just simple ABAP based on TYPE d or datum (abap type date) using character replacement as well as addition/subtraction:
*&---------------------------------------------------------------------*
*& Report ZDEMO_DATE_FIRST_LAST_MONTH
*&---------------------------------------------------------------------*
REPORT zdemo_date_first_last_month.
PARAMETERS p_date TYPE d DEFAULT sy-datum. " the entered date
DATA tempdate TYPE d. " for calculation purposes
**********************************************************************
* 1) First and last of current month based on selected date
**********************************************************************
DATA firstofcurrmonth TYPE d. " the first of current month based on selected date
DATA lastofcurrmonth TYPE d. " the last of current month based on selected date
" 1.a) First of current month based on selected date
firstofcurrmonth = p_date(6) && '01'. " We know each month starts on the 1st, this one is easy
" 1.b) Last of current month based on selected date
tempdate = p_date(6) && '28'. " we know each month has at least 28 days
tempdate = tempdate + 4. " adding 4 days will bring us always into the next month
tempdate = tempdate(6) && '01'. " this will give us the first of the next month
lastofcurrmonth = tempdate - 1. " subtracting one, will give us the last of current month
**********************************************************************
* 2) First and last of previous month based on selected date
**********************************************************************
DATA firstofprevmonth TYPE d. " the first of previous month based on selected date
DATA lastofprevmonth TYPE d. " the last of previous month based on selected date
" 2.a) Last of previous month based on selected date
" We already know the first of the current month, so simply subtract one
lastofprevmonth = firstofcurrmonth - 1.
" 2.b) First of previous month based on selected date
firstofprevmonth = lastofprevmonth(6) && '01'.
**********************************************************************
* 3) First and last of next month based on selected date
**********************************************************************
DATA firstofnextmonth TYPE d. " the first of next month based on selected date
DATA lastofnextmonth TYPE d. " the last of next month based on selected date
" 3.a) First of next month based on selected date
" We could have grabbed this date already from step 1.b)
firstofnextmonth = lastofcurrmonth + 1. " But we can also simply add 1 to the last of the current month
" 3.b) Last of next month based on selected date
tempdate = firstofnextmonth(6) && '28'. " same logic as in step 1.b)
tempdate = tempdate + 4. " adding 4 days will bring us always into the next month
tempdate = tempdate(6) && '01'. " this will give us the first of the next, next month
lastofnextmonth = tempdate - 1. " subtracting one, will give us the last of next month
**********************************************************************
* Display dates
**********************************************************************
cl_demo_output=>write( p_date ).
cl_demo_output=>write( firstofcurrmonth ).
cl_demo_output=>write( lastofcurrmonth ).
cl_demo_output=>write( firstofprevmonth ).
cl_demo_output=>write( lastofprevmonth ).
cl_demo_output=>write( firstofnextmonth ).
cl_demo_output=>write( lastofnextmonth ).
cl_demo_output=>display( ).
07-14-2020 8:05 PM
Thanks for the code. But, I am trying to call the method using the class to display the output but it is saying "cl_demo_output" does not exist.
07-14-2020 9:32 PM
07-14-2020 1:40 PM
I think that these function modules are not released. Anyway, no need of any function module, it's very easy to use classic ABAP statements for each of the questions.