Macro to validate Date

Content Author : Abhishek Kumar
Email Address  : abhishek.kumar@thaneweb.com

*======================================================================*
* Programme   : zdate_validate                                         *
* Title       : Macro to validate Date                                 *
* Author      : Abhishek Kumar                                         *
* Description : This program has two macros and a sample code which is *
*               showing how to use that.                               *
*               Macro 1. > Val_date                                    *
*               Macro 2. > Valdate                                     *
*               It return SY-Subrc value if it 0 then the entered date *
*               is valid else either the date is invalid or the        *
*               specified format is invalid                            *
*----------------------------------------------------------------------*
* Created and Designed by Abhishek Kumar in Nov 2001.                  *
*----------------------------------------------------------------------*
*Abhishek is not responsible for any damages caused by the use or      *
*misuse of this program and cannot provide any warranty with this      *
*program. Use it entirely at your own risk.                            *
*                                                                      *
*You are not authorised to make any changes without prior written      *
*permission from the author.                                           *
*======================================================================*

PROGRAM ZDATE_VALIDATE .
* Validation of Date
DEFINE VAL_DATE.
  CLEAR: %_DATE1,
         %_DATE2.
  %_DATE1(4)   = &1. "Year
  %_DATE1+4(2) = &2. "Month
  %_DATE1+6(2) = &3. "Date
  %_DATE2 = %_DATE1 - 1.
  %_DATE2 = %_DATE2 + 1.
  IF %_DATE1 <> %_DATE2.
    SY-SUBRC = 1.
  ELSE.
    SY-SUBRC = 0.
  ENDIF.
END-OF-DEFINITION.

DEFINE VALDATE.
***************************************************
* Passing Parameters: &1 - Date
*                     &2 - Date Format
*
* Date Format:
* DDMMYYYY  MMDDYYYY  YYYYMMDD  YYYYDDMM
*
* SY-SUBRC Return Value:
*                     1 invalid date
*                     0 Valid Date
*                     2 Invalid Format
***************************************************
  DATA: %_DATE1 LIKE SY-DATUM  ,
        %_DATE2 LIKE SY-DATUM ,
        %_DATE3(8).
  case &2.
    when 'DDMMYYYY'.
      val_date &1+4(4) &1+2(2) &1+0(2).
    when 'MMDDYYYY'.
      val_date &1+4(4) &1+0(2) &1+2(2).
    when 'YYYYMMDD'.
      val_date &1+0(4) &1+4(2) &1+6(2).
    when 'MMYYYYDD'.
      val_date &1+2(4) &1+0(2) &1+6(2).
    when others.
      sy-subrc =  2.
  endcase.
END-OF-DEFINITION.

***********************************************
*      SAMPLE USE of above MACRO              *
***********************************************

DATA: V_DATE(8).
V_DATE = '30022002'.
*=>
VALDATE V_DATE 'DDMMYYYY'.
*=>
IF SY-SUBRC = 0.
  WRITE:/ 'Valid Date'.
ELSEIF SY-SUBRC = 1.
  WRITE:/ 'Invalid Date'.
ELSEIF SY-SUBRC = 2.
  WRITE:/ 'Invalid Format'.
ENDIF.
*********************************************************************

*-- End of Program

Get help for your ABAP problems
Do you have a ABAP Question?

SAP Books
SAP Certification, Functional, Basis Administration and ABAP Programming Reference Books

More ABAP Tips

Best regards,
SAP Basis, ABAP Programming and Other IMG Stuff
http://www.erpgreat.com

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.  The site www.erpgreat.com is in no way affiliated with SAP AG. 
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk. 
 The content on this site may not be reproduced or redistributed without the express written permission of 
www.erpgreat.com or the content authors.