Skip to Content
-3

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.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

1 Answer

  • Best Answer
    Posted on Jul 14, 2020 at 11:04 AM

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


    Add a comment
    10|10000 characters needed characters exceeded

    • Sai Srinath Mummaka, 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?

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.