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

Different method of Converting Date in ABAP

Calculate begining of previous to end of previous month

ldate = sy-datum.
ldate+6(2) = '01'.
ldate = ldate - 1.
fdate = ldate.
fdate+6(2) = '01'.

move: fdate to date-low,
ldate to date-high.

append date.

------

I need to convert date from dd.mm.yyyy to yyyymmdd at runtime.

I do not know if there is any function module or not but try following code.

/*************************************************
/**************************************************

data mydate like sy-datum.
data: year(4) type c,
month(2) type c,
date(2) type c.

year = mydate(4).
month = mydate+4(2).
date = mydate+6(2).

write: / year no-gap, month no-gap, date.

/*************************************************
/**************************************************

I am facing one problem with date. Can you suggest me how to save date with some format . for ex: can I save '2004.12.01' in the table with the same format. Right now I am getting output '2...1.2004' but I need output '2004.12.01'.

Date will save in any like yyyymmdd.

Just for display only you can do whatever ever you want.

write:/ gv_date edit mask 'YYYY.MM.DD'.

Thanks for your suggestion..but I think you have not properly understood the problem..Pls don't mind...to make you aware of the problem, I am writing a small coding...

data : date(10) type c value '01122004'.

write :/ date using edit mask '__/__/____'.

output is : 01.12.2004.

but if will pass the value like 01.12.2004 or 01/12/2004 or if change the format i.e 20041201.....then I will get output as 2...1.2004

Pls it would be very helpful if you can kindly devote some time for me.

Have you tried to use CONCATENATE to format the date?

E.g: CONCATENATE: T_0008-BEGDA+6(02)
'/'
T_0008-BEGDA+4(02)
'/'
T_0008-BEGDA(04) INTO T_AUX-DT_ALT.

The result will be 30/12/9999. Just change the IT's for your convenience.

1) Can anyone pls tell me fm to calculate the difference between two dates and return no.of days
as o/p.its very urgent.

2) I want to display all the date calulations, other calculations in one internal table for alv grid display.is it possible.

1. Use the fm 'DAYS_BETWEEN_TWO_DATES'.
2. It is possible. You need to take it in the final internal table which you are going to display.

Understanding Date Selections Using the HR Logical Database

sample screen- click to view an example screen. This might make the description below a little clearer.

When you enter dates into the selection screen of an HR logical database, it is important to know what each selection means, and what SAP does with the entries.

The key values provided by the Logical Database are the start and end dates. These values are stored in the values PN/BEGDA (Start Date) and PN/ENDDA (End Date). Depending on what option the user chooses in the Period box, different values are put in PN/BEGDA and PN/ENDDA.

An example will make things much more clear. Assume that today is June 5, 2000 (2000/06/05). The list below shows what the value of PN/BEGDA and PN/ENDDA will be for each of the six options.


Option


Description


PN/BEDGA


PN/ENDDA

TodayUses the current date for both of PN/BEGDA and PN/ENDDA.2000060520000605
Current MonthFrom the first day of the month to the last day of the month2000060120000630
Current YearFrom the first day of the year to the last day of the year2000010120001231
Up to TodayFrom the first day of the year to the current date2000010120000605
From TodayFrom the current date to the end of the year2000060520001231
Other periodWhen only one value is entered, of PN/BEGDA and PN/ENDDA and set to the same value. Otherwise of PN/BEGDA is the first field, and PN/ENDDA is the second fieldN/AN/A

Person Selection Period

When you enter a person selection period, the system selects only those employees who are members of the company on at least one day in the specified period. These are persons with a valid Organizational Assignment (0001) record. Entries in the standard selection options fields limit the personnel numbers that are selected.

You can specify an interval by entering the 'from' and 'to' dates in the left and right columns respectively.

The following reporting options are available:

The entire period - If you make no entries, the system sets the lowest and highest system date as the 'from' and 'to' dates.

Part periods - You can specify an interval by making an entry in the left and right columns. If you enter only the highest date, the system automatically sets the lowest date.

The values PN/BEGDA and PN/ENDDA are used to bring back a list of employees that meet the date parameters, plus any other selection criteria that may be entered elsewhere on the selection screen.

sample program1

REPORT  y_exemple.                             

TABLES : ttdtg.

DATA : gv_day TYPE p,
gv_day_num(2) TYPE n,
gv_name_var(20) VALUE '%%SAPSCRIPT_DDDD_00'.

* Saisie date
parameters : p_date like sy-datum obligatory.

* Récupération du no du jour de la semaine
CALL FUNCTION 'DAY_IN_WEEK'
EXPORTING
datum = p_date
IMPORTING
wotnr = gv_day.

* Tranduction de ce no en jour
* Dans la table TTDTG, on a toutes les chaines de caractères utilisées
* par SAPscript pour traduire les dates, on les utilise pour notre ex
IF sy-subrc = 0.
gv_day_num = gv_day.
gv_name_var+17(2) = gv_day_num.
CLEAR ttdtg.
SELECT SINGLE * FROM ttdtg
WHERE spras = sy-langu
AND varname = gv_name_var.

WRITE : /'La date du ', p_date, 'correspond à un', ttdtg-varvalue.
ENDIF.

Substring in SAP's ABAP programming

Unlike many of the higher level languages there is no stand-alone substring call. We have split, shift, replace, translate and condense but no substring. So if we want to know the first four characters from the start of a field how do we do it?

The WRITE statement is what we use to substring a field. The syntax is as follows:

WRITE fieldname+starting_position(field_length) to variable


The fieldname is the source (or input). The plus sign precedes the starting position of the substring. IMPORTANT The traditional method of counting on computers is followed. The first position in the string is 0. Immediately after (no space) comes the length of the substring you are going to use. You can also substring the variable that you are writing the string to (the target). I have included an example that illustrates substringing on the source and target fields.

A real-life use of the substring functions

I needed to build a character date field for a cross-platform integration project I was working on. First, I defined the date field:

data: datechar(10).

To build the date lets use sy-datum, which has the current system date.

  write SY-DATUM+4(2) to datechar+0(2).
write '/' datechar+2(1).
write SY-DATUM+6(2) to datechar+3(2).
write '/' TO datechar+5(1).
write SY-DATUM+0(4) to datechar+6(4).

Value of SY-DATUM 20010616

Here's how the field changes with the execution of each line.

06________
06/_______
06/16_____
06/16/____
06/16/2001
This is just one of the great functionalities of WRITE.

Date and Time Calculations

Date and time fields have character types, not numeric ones. However, you can still use date and time fields in numeric operations. To allow you to do so, the system performs automatic type conversions. You may also find it useful to use offset and length specifications when using date and time fields in calculations.


Example of a date calculation:

DATA: ULTIMO TYPE D.

ULTIMO = SY-DATUM.
ULTIMO+6(2) = '01'. " = first day of this month
ULTIMO = ULTIMO - 1. " = last day of last month

Here, the last day of the previous month is assigned to the date field ULTIMO.

1. ULTIMO is filled with the present date.

2. Using an offset specification, the day is changed to the first day of the current month.

3. 1 is subtracted from ULTIMO. Its contents are changed to the last day of the previous month. Before performing the subtraction, the system converts ULTIMO to the number of days since 01.01.0001 and converts the result back to a date.


Example of a time calculation:

DATA: DIFF TYPE I,
SECONDS TYPE I,
HOURS TYPE I.

DATA: T1 TYPE T VALUE '200000',
T2 TYPE T VALUE '020000'.

DIFF = T2 - T1.
SECONDS = DIFF MOD 86400.
HOURS = SECONDS / 3600.

The number of hours between 02:00:00 and 20:00:00 is calculated.

1. First, the difference between the time fields is calculated. This is -64800, since T1 and T2 are converted internally into 72000 and 7200 respectively.

2. Second, with the operation MOD, this negative difference is converted to the total number of seconds. A positive difference would be unaffected by this calculation.

3. Third, the number of hours is calculated by dividing the number of seconds by 3600.

The last three lines can be replaced by the following line

HOURS = ( ( T2 - T1 ) MOD 86400 ) / 3600.

Program 1

*
* 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.

Program2

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.

Tutorials on SAP-ABAP

Adobe Interactive Forms Tutorials

Business Server Pages (BSP)

Userexits/BADIs

Web Dynpro for ABAP (Step by step procedure for web dynpro,Tutorials on Web Dynpro,)

ALV Tutorials

goodsites