Elegance in ABAP: date calculations

As you might see in different ABAP forums people ask repeatedly for a function module doing some simple date calculation, e.g. end of month or weekday calculation. The first question, the rhetorical one – “why on earth they cannot search the SAP repository themselves” – definitely has no answer. We must assume that there is a kind of people whose mentality prevents them from trying to solve problems themselves; they always seek help from others in their everyday duties.

The other question is why at all one needs an FM for these particular calculations. Just think: ABAP has a special data type for dates, and it has built-in efficient date arithmetic, so why not to use it.

For example, if you need to add a number of days to a date, you can use a simple ADD statement or arithmetic ‘+’ operation, where one operand is of type d and the other is of type integer.

Another example is weekday calculation. Knowing that 01/01/1900 was Monday, you can always calculate a weekday by the simple statement:

The result will be the number of range from 1 to 7, where 1 is Monday and 7 is Sunday. This calculation will work also correctly for dates before 01/01/1900. You can use the result as a key for T246 SAP table, which stores weekday names in different languages.

A little harder is end-of-month calculation. By the way, you can find in SAP system repository several FMs calculating an end-of-month day, with more or less correct leap year calculation. However, the thing is that having built-in ABAP date arithmetic you hardly need an FM, and you don’t need to calculate a leap year.

Suppose, you have a variable SOMEDATE with arbitrary date. First, we calculate the first day of a month (remember, internally date is represented in ABAP as YYYYMMDD):

SOMEDATE+6(2) = ‘01'.

Next, we have to find some date in the next month. Knowing the first day of the month, we can add, for example, 31 days to that day, and the result will obviously be in the next month boundaries:

ADD 31 TO SOMEDATE.

Next, let’s again calculate the first day of the month (in this step this will be the next month):

SOMEDATE+6(2) = ‘01'.

Now as we have the first day of the next month we can just subtract 1 day, and the result will be our end-of-month day:

Thanks for the comment. Yes, there are many FMs in SAP for date calculations. My point was just that you not always need them. For example, the equivalent of CONVERT_DATE_TO_EXTERNAL would be just WRITE somedate TO string, etc.

I totally agree with your statement that nowadays consultant dont go for searching the simple solution. And the easy way they find is put up the query on SDN… even not cared to search SDN itself for the similar query and it’s resolution.

There are many such Date related calculation that can be done first hand in EXCEL and the same can be converted to ABAP. I have done most of my date calculation in similar fashion. And i have surely done that last day of month.. but in a little different way, instead of adding days .. i added 1 month to someday+4(2) and then make it first day of that month … next doing a simple 1 day minus.

Good to find someone who really thinks about simple solution then looking at complex problems.

The code snippet will work correctly for any date, as the first line of it converts our given date to the start of the month. So, in case SOMEDATE = ‘20090129’, after the first line of code SOMEDATE will be equal to ‘20090101’, and adding to it 31 day will give us ‘20090201’. Try it yourself in your program.

Your blog is simple but thoughtful. However, finally you suggest to have this wrapped in a macro or something like that …. leading to the same idea of “Function Modules” … so instead of using SAP standard routines, I would end up writing my own functions. But if we take the basic concept of understanding date data-type and its in-built attributes and behaviour… it would help a lot.

Hi Suresh,Thanks for your comment. For simple date calculations I prefer macros, not FMs, as macros have zero calling cost (actually it is not a call, macros are resolved during program source compilation).

Found this blog a while ago and bookmarked it. Today was the day where I could put it to use. I really like the lean and mean style of date calculations. Why waste time on overhead if writing it directly saves not only time but also lines of code.