Professional Documents
Culture Documents
Blogs
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:
SOMEDATE+6(2) = ‘01'.
ADD 31 TO SOMEDATE.
SOMEDATE+6(2) = ‘01'.
SUBTRACT 1 FROM SOMEDATE.
No IF's, no CASE's. The good idea is to encapsulate this code snippet into a parametric macro definition; no need for another function module as the cost of its call will be far beyond the payload.
And what about elegance? Frankly, I don't know, it was just to catch your eye.
Sergey Korolev is a self-employed ABAP developer.
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.
Cheers Carsten
Great Blog
2010-02-11 10:58:43 Sergey Korolev Business Card [Reply]
Cheers
Sergey
Date Calculations
2010-01-04 17:34:28 Suresh Radhakrishnan Business Card [Reply]
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.
Cheers
Suresh
Date Calculations
2010-01-05 05:39:58 Sergey Korolev Business Card [Reply]
Hi Suresh,
http://weblogs.sdn.sap.com/pub/wlg/17269 19/2/2010
SAP Network Blog: Elegance in ABAP: date calculations Page 2 of 3
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).
Regards,
Sergey
Other point
2009-12-29 05:13:00 Steven Oldner Business Card [Reply]
Looking at those function modules, one only needs to use SE37 and put in either *date* or *month* to find them. WE ought to make a "Let me google that for you" for R/3.
good one...
2009-12-29 00:55:53 Rajesh Salecha Business Card [Reply]
Hi,
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.
good one...
2009-12-29 01:14:32 Sergey Korolev Business Card [Reply]
Hi Raj,
Thanks for the comment. I just want to note that adding 1 to someday+4(2) is not correct, as it produces 13th month for December, and thus you need IF to make correct calculations.
good one...
2010-01-05 21:34:27 Rajesh Salecha Business Card [Reply]
Hi Sergey,
Just was thinking that your provided code may fail if the date in concern are 29th / 30th / 31st Jan...
In that case if you add 31 days it will directly move to March skipping the Feb.
i.e.
for 29th Jan 2009
This is just for your information so that in case if someone uses this code , he/she may have a second thought for this scenario..
good one...
2010-01-05 22:46:26 Sergey Korolev Business Card [Reply]
Hi Rajesh,
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.
Regards,
Sergey
I agree that most of the people are bit lazy to search the forum for the FMs and keep requesting for those.
But date calculations are not so simple as you mentioned in your blog. They can be combination of calendars, fiscal years, working days and non working days etc all can come into action.
Thanks for Manas for mentioning out some of the date functions.
Thanks,
Ram Bhupal
http://weblogs.sdn.sap.com/pub/wlg/17269 19/2/2010
SAP Network Blog: Elegance in ABAP: date calculations Page 3 of 3
Hi,
Yes, I see your point. For example, you cannot do without FMs when dealing with factory calendars or employee calendar. But frankly, I never use FM to calculate the end of calendar
month.
Kind regards,
Sergey Korolev
Enjoyed reading ur blog. The elegance part was the best one :)
Regards
Manas Dua
Hi Manas,
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.
Best regards,
Sergey Korolev
http://weblogs.sdn.sap.com/pub/wlg/17269 19/2/2010