Skip to Content
Technical Articles

tips of date check and convert date with month text

One small tip for a date validation check and convert into text with month text(like convert 06-14-2021 to 14th Jun 2021):

 

 

 

  1. using function module ‘DATE_CHECK_PLAUSIBILITY’ to check if it’s the valid date;
  2. get month texts from table t247 with a language key then concatenate.

*---------------------------------------------------------------------*
*       FORM format_data_with_text                                    *
*---------------------------------------------------------------------*
FORM format_data_with_text USING    ip_date
                           CHANGING op_text.
  DATA: lv_stext(3),
        lv_date LIKE sy-datum,
        lv_fcmnr TYPE fcmnr,
        lv_suffix(2).

  lv_date = ip_date+0(8).
  CALL FUNCTION 'DATE_CHECK_PLAUSIBILITY'
    EXPORTING
      date                      = lv_date
    EXCEPTIONS
      plausibility_check_failed = 1
      OTHERS                    = 2.

  IF sy-subrc <> 0.
    op_text = ip_date.
  ELSE.
    CASE lv_date+7(1).
      WHEN '1'.
        lv_suffix = 'st'.
      WHEN '2'.
        lv_suffix = 'nd'.
      WHEN '3'.
        lv_suffix = 'rd'.
      WHEN OTHERS.
        lv_suffix = 'th'.
    ENDCASE.

    CLEAR: lv_stext.
    lv_fcmnr = lv_date+4(2).
    SELECT SINGLE ltx
      INTO lv_stext
      FROM t247 WHERE spras EQ 'EN'
                    AND mnr EQ lv_fcmnr.

    CONCATENATE lv_date+6(2) lv_suffix  INTO op_text.
    CONCATENATE op_text lv_stext lv_date+0(4)
    INTO op_text SEPARATED BY space. " with date suffix
    "CONCATENATE lv_date+6(2) lv_stext lv_date+0(4)
    "INTO op_text SEPARATED BY space. "no date suffix
  ENDIF.
ENDFORM.

Thanks, Vicenç Xavier Lozano let me know the class ‘CL_RECA_DATE’ which provides many such kinds of covert methods.

Method Name Description
ADD_MONTHS_TO_DATE Adds Month to Date
ADD_TO_DATE Adds to Date
AS_CHAR Converts Date into CHAR Field
CALCULATE_BUSINESS_DATE Determination of Working Day
CHECK_DATE Checks Validity of Date Field
CHECK_INTERSECTION Checks Overlapping
CHECK_PERIOD Checks Period (Correct Date, from > to, transfer, …)
CHECK_PERIODS_OF_TABLE Checks Periods of Table Generically
CONVERT_DATE_TO_INTERNAL Converts Date to Internal Format
CONVERT_DATE_TO_STRING Converts the Date into a String (Text Field)
CONVERT_DATE_TO_STRING_X Converts Date to String (Weekday, Date)
CONVERT_RANGE_TO_STRING Converts Time Period to String
CREATE Determines New Date from Date and Interest/Calendar Days
CREATE_DATE_RANGES Create Overlapping Time Periods
CREATE_DATE_RANGES_COMPRESSED Compress Time Periods
CUT_DATE_RANGES Determine Smallest Time Periods
FILL_DATE_TABLE Fills Period Table Using Reference
GET_CALENDAR_UNITS Gets Units
GET_CURRENT_DATE Supplies Current Date and Time (UTC Converted)
GET_DATE_DIFF Gets the Number of Years/Months/Days from Date 1 to Date 2
GET_DATE_INFO Gets Month/Year/Period for Date
GET_DAYS_BETWEEN_TWO_DATES Calculates Number of Days Between Two Dates
GET_INTERSERCTING_PERIOD Gets Period of Overlap Between Two Time Periods
GET_LEAP_DAYS_BETWEEN_2_DATES Calculates Number of Leap Days Between Two Dates
GET_WEEKDAY Gets Weekday (Text)
GET_WEEKDAY_SHORT Gets Weekday (2 Character Text)
GET_WEEK_INFO_BY_DATE Gets Information on Week
IS_DATEFROM_INITIAL Bool: From-Date = “Initial” or “Zero”
IS_DATETO_INITIAL Bool: To-Date = “Initial” or “Ulimited”
IS_DATE_INITIAL Bool: Date = “Initial”
IS_DATE_OK Is the Date a Valid Date?
MERGE_TABLES Merges the Dates of Two Tables
MONTHS_BETWEEN_TWO_DATES Calculates Number of Months Betw. 2 Calendar Dates
SET_TO_BEGIN_OF_MONTH Sets Date to Beginning of Month
SET_TO_END_OF_MONTH Sets Date to End of Month
SET_TO_NEXT_DAY Sets Date to Next Day
SPLIT_TABLE_IN_DATERANGE Splits Table Based on Time Period Table (Generic)
SUB_MONTHS_FROM_DATE Subtracts Months from Date
END_OF_MONTH_DETERMINE Determines If Date Is the End of the Month

 

3 Comments
You must be Logged on to comment or reply to a post.