Subscribe

RSS Feed (xml)



Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Sunday, August 30, 2009

Convert month value of a date to text


The following ABAP code gets the month value of a particular date (YYYYMMDD) and converts
it to its text value. I know its a very crude way of doing it but it does the job and having the
ABAP code below provides a quick and easy way to copy and paste it into your SAP program.


*

DATA: gd_datetext TYPE string.

CASE sy-datum+4(2).
WHEN '01'.
gd_datetext = 'January'.
WHEN '02'.
gd_datetext = 'February'.
WHEN '03'.
gd_datetext = 'March'.
WHEN '04'.
gd_datetext = 'April'.
WHEN '05'.
gd_datetext = 'May'.
WHEN '06'.
gd_datetext = 'June'.
WHEN '07'.
gd_datetext = 'July'.
WHEN '08'.
gd_datetext = 'August'.
WHEN '09'.
gd_datetext = 'September'.
WHEN '10'.
gd_datetext = 'October'.
WHEN '11'.
gd_datetext = 'November'.
WHEN '12'.
gd_datetext = 'December'.
ENDCASE.

concatenate sy-datum(2) gd_datetext sy-datum+2(2) into gd_datetext
separated by space.

Pos

Convert month value of a date to text


The following ABAP code gets the month value of a particular date (YYYYMMDD) and converts
it to its text value. I know its a very crude way of doing it but it does the job and having the
ABAP code below provides a quick and easy way to copy and paste it into your SAP program.


*

DATA: gd_datetext TYPE string.

CASE sy-datum+4(2).
WHEN '01'.
gd_datetext = 'January'.
WHEN '02'.
gd_datetext = 'February'.
WHEN '03'.
gd_datetext = 'March'.
WHEN '04'.
gd_datetext = 'April'.
WHEN '05'.
gd_datetext = 'May'.
WHEN '06'.
gd_datetext = 'June'.
WHEN '07'.
gd_datetext = 'July'.
WHEN '08'.
gd_datetext = 'August'.
WHEN '09'.
gd_datetext = 'September'.
WHEN '10'.
gd_datetext = 'October'.
WHEN '11'.
gd_datetext = 'November'.
WHEN '12'.
gd_datetext = 'December'.
ENDCASE.

concatenate sy-datum(2) gd_datetext sy-datum+2(2) into gd_datetext
separated by space.

Pos

Add n number of working days to date

Simply add the below FORM into you ABAP code and call it using the usual PERFORM command:
   DATA: ld_date TYPE sy-datum.
PERFORM calculate_date using '-4'
changing ld_date.



*&---------------------------------------------------------------------*
*& Form CALCULATE_DATE
*&---------------------------------------------------------------------*
* Add/Subtract n number of months from a date
*----------------------------------------------------------------------*
* --> p_months Number of months to add/subtract
* <-- p_date Start date and result date
*----------------------------------------------------------------------*

FORM calculate_date USING p_months
CHANGING p_date.

DATA: ld_datestor TYPE sy-datum.

ld_datestor = p_date.

CALL FUNCTION 'MONTH_PLUS_DETERMINE'
EXPORTING
MONTHS = p_months
OLDDATE = p_date
IMPORTING
NEWDATE = p_date.
ENDFORM. " CALCULATE_DATE

Saturday, August 29, 2009

Add n number of working days to date (using SAP personal work schedule)

The following ABAP code adds n number of WORKING days to a particular date, but allows result to be a non
working day. For example if starting day is Wednesday and you add three days to it, resultant day would be
Saturday(non working day). Where as if you wanted to totally ignore non working days resultant day would be
Monday (see ABAP code to totally ignore non working days).

Simply add the below ABAP FORM into you code and call it using the usual PERFORM command:
   PERFORM add_working_days_resnonwork USING ld_numdays
CHANGING gd_date.



**&---------------------------------------------------------------------*
*& Form ADD_WORKING_DAYS_RESNONWORK
*&---------------------------------------------------------------------*
* Add n number of factory days(working days) to date, but allow
* resultant date to be a non working day
*----------------------------------------------------------------------*
* <-- P_DAYS Number of days to add * <-- P_PAYDATE Starting date *----------------------------------------------------------------------*
FORM add_working_days_resnonwork USING p_days
CHANGING p_paydate TYPE sy-datum.

DATA: ld_count TYPE i.

ld_count = p_days.

WHILE ld_count GT 0.
CALL FUNCTION 'DATE_CHECK_WORKINGDAY'
EXPORTING
date = p_paydate
factory_calendar_id = 'GB'
message_type = 'I'
EXCEPTIONS
date_after_range = 1
date_before_range = 2
date_invalid = 3
date_no_workingday = 4
factory_calendar_not_found = 5
message_type_invalid = 6
OTHERS = 7.
IF sy-subrc NE 4.
p_paydate = p_paydate + 1.
ld_count = ld_count - 1.
ELSE.
p_paydate = p_paydate + 1.
ENDIF.
ENDWHILE.

ENDFORM. " ADD_WORKING_DAYS_RESNONWORK

Add n number of working days to date

The following ABAP code adds n number of WORKING days to a particular date, any days which are not
workings days (i.e. Saturday) will be ignored. For example if your starting date is Friday, adding 3 days
would return a result of Wednesday or if starting date was Wednesday resultant day would be Monday as
Saturday and Sunday are non working days. If you want to add n number of working days but allow result
to be a non working day then click here for alternative ABAP code.

Simply add the below FORM into you code and call it using the usual PERFORM command:
   PERFORM add_working_days USING ld_numdays
CHANGING gd_date.



**&---------------------------------------------------------------------*
*& Form ADD_WORKING_DAYS
*&---------------------------------------------------------------------*
* Add n number of factory days(working days) to date
*----------------------------------------------------------------------*
* <-- P_DAYS Number of days to add * <-- P_PAYDATE Starting date *----------------------------------------------------------------------*
FORM add_working_days USING p_days
CHANGING p_paydate TYPE sy-datum.
DATA: gd_factorydat LIKE scal-facdate,
gd_resdate LIKE sy-datum.

* Convert date to factory date
CALL FUNCTION 'DATE_CONVERT_TO_FACTORYDATE'
EXPORTING
date = p_paydate "Starting date
factory_calendar_id = 'GB'
IMPORTING
factorydate = gd_factorydat. "Factory calender date

* Add n number of days to factory date, ignors non working days
gd_factorydat = gd_factorydat + p_days.

* Convert factory date back to actual date
CALL FUNCTION 'FACTORYDATE_CONVERT_TO_DATE'
EXPORTING
factorydate = gd_factorydat
factory_calendar_id = 'GB'
IMPORTING
date = gd_resdate. "Actual date

p_paydate = gd_resdate.
ENDFORM. " ADD_WORKING_DAYS

Pop a Date in ABAP Report Selection Screens

*
* Pop a Date in selection screens for the users to choose the month and
* year
*
* Written by : SAP Basis, ABAP Programming and Other IMG Stuff
*
*
REPORT ZPOPDATE.

DATA: V_CODE LIKE SY-SUBRC.

PARAMETER: V_MONTH LIKE ISELLIST-MONTH.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR V_MONTH.

CALL FUNCTION 'POPUP_TO_SELECT_MONTH'
EXPORTING
ACTUAL_MONTH = '200205'
LANGUAGE = SY-LANGU
START_COLUMN = 8
START_ROW = 5
IMPORTING
SELECTED_MONTH = V_MONTH
RETURN_CODE = V_CODE
EXCEPTIONS
FACTORY_CALENDAR_NOT_FOUND = 1
HOLIDAY_CALENDAR_NOT_FOUND = 2
MONTH_NOT_FOUND = 3
OTHERS = 4.

*--------Display different date formats with popups-----------------*

REPORT zdate .

DATA: l_code LIKE sy-subrc.
DATA: lv_date(10) TYPE c.
DATA: BEGIN OF lwa_date OCCURS 0,
lv_d(2) TYPE c VALUE '1',
END OF lwa_date.

PARAMETER: p_month LIKE isellist-month.
PARAMETER: p_date(2) TYPE c.

AT SELECTION-SCREEN.

INITIALIZATION.
DO 31 TIMES.
APPEND lwa_date TO lwa_date.
lwa_date-lv_d = lwa_date-lv_d + 1.
WRITE lwa_date-lv_d.
ENDDO.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_month.

CALL FUNCTION 'POPUP_TO_SELECT_MONTH'
EXPORTING
actual_month = '200505'
language = sy-langu
start_column = 8
start_row = 5
IMPORTING
selected_month = p_month
return_code = l_code
EXCEPTIONS
factory_calendar_not_found = 1
holiday_calendar_not_found = 2
month_not_found = 3
OTHERS = 4.




AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_date.

CHECK NOT lwa_date[] IS INITIAL.
CALL FUNCTION 'POPUP_WITH_TABLE'
EXPORTING
endpos_col = 5
endpos_row = 10
startpos_col = 2
startpos_row = 1
titletext = 'DATE'
IMPORTING
choice = lwa_date
TABLES
valuetab = lwa_date
EXCEPTIONS
break_off = 1
OTHERS = 2.
p_date = lwa_date-lv_d.


END-OF-SELECTION.
  CONCATENATE    p_date '/' p_month+4(2) '/' p_month+0(4) INTO lv_date.
WRITE:/ 'IN DD/MM/YYYY', lv_date.
CONCATENATE p_month+4(2) '/' p_date '/' p_month+0(4) INTO lv_date.
WRITE:/ 'IN MM/DD/YYYY', lv_date.
CONCATENATE p_month+0(4) '/' p_month+4(2) '/' p_date INTO lv_date.
WRITE:/ 'IN YYYY/MM/DD', lv_date.
*end of program.

ABAP Date Time Variables and Addressing Subfields

Date and Time Data types.

Type d is the data type used for dates and type t for time. Both consist of string representations of dates (in a YYYMMDD format) and times (HHMMSS) with which calculations can be performed. Adding an integer value to a type d variable affects the day part of the date while adding an integer to a time value affects seconds. Assigning a date or time value to an integer yields the number of days since 01.01.0001 or the number of seconds since 00:00:00. The system date is provided by the sy-datum system field while the system time by sy-uzeit.

Addressing subfields

Extracting date and time fields from date and time variables can be achieved by using the SAP sub-field syntax which -- given a filed named f -- goes like this :

subfield = f[+offset][(length)]

if you specify an offset without length then the entire string starting from offset is addressed. if no offset is specified then 1 is implied. ABAP strings always start at index 1.

Examples

Keeping in mind that the internal format of a date field is YYYYMMDD we may declare some variables using the following syntax
DATA :
my_birthday TYPE d VALUE '19681120',
cur_year TYPE i VALUE sy-datum(4), " get the first four characters of the sy-datum field
cur_month TYPE i VALUE sy-datum+4(2), " get the two characters after position 4 i.e. 5 and 6
cur_day TYPE i VALUE sy-datum+6(2) " get the two characters after positi