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: 

Date Validation and Printing

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.

1 ACCEPTED SOLUTION

michael_piesche
Active Contributor

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( ).

5 REPLIES 5

ziolkowskib
Active Contributor

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

michael_piesche
Active Contributor

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( ).

0 Kudos

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.

smummaka, dont worry too much about this SAP standard class not being existing in your system. I just added that to help visualize the output.The class CL_DEMO_OUTPUT is part of the SAP package SABAP_DEMOS_OUTPUT_STREAM, and I have it present in a SAP CRM 731 application, a SAP ERP 6.0 740, as well as in a S/4 HANA 754. Just wondering, what is your SAP application and what is the SAP_BASIS or the SAP Netweaver release?

Sandra_Rossi
Active Contributor
0 Kudos

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.