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:

CONSTANTS: c_known_mondayTYPEdVALUE'19000101'. "It's Monday weekday = ( given_Date - c_known_monday ) mod 7 + 1.

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:

ADD31TOSOMEDATE.

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:

SUBTRACT1FROMSOMEDATE.

So, the calculation consists of just four statements:

SOMEDATE+6(2) = ‘01'.ADD31TOSOMEDATE. SOMEDATE+6(2) = ‘01'.SUBTRACT1FROMSOMEDATE.

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.

a) DATE_COMPUTE_DAY – Finds day of the month

b) DATE_CONV_EXT_TO_INT - user formatted date is converted to system date

c) DATE_GET_WEEK - convert date into year + week format

d) DATE_TO_DAY – gives weekday from date

e)DATE_IN_FUTURE – takes number of days and date - gives future date in user format and system format

f) MONTH_PLUS_DETERMINE – add or subtract month from a date

g) RP_CALC_DATE_IN_INTERNAL – add/subtract year/month/days from a date

h) WEEK_GET_FIRST_DAY – take input as YYYYWW and it gives first day of the week.

i) MONTH_NAMES_GET – language is only parameter. Returns internal table with months.

j) MONTH_PLUS_DETERMINE – subtract months from date

k) CONVERT_DATE_TO_EXTERNAL – internal date to user date

l) HRVE_CONVERT_TIME – converts the 12 hour time format to 24 hr time format

I personally maintain a document of all frequently used Function Modules...

Regards

Manas Dua

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

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

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

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.

All the best.

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.

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

29/Jan/09 + 31 days= 01/Mar/09

then changing the first day to 01 and doing minus 1 will give the result 28th or 29th Feb based on leap year, which is wrong.

The solution is to still have one if- statement check.

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

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

Enjoy your answers, thanks!

Cheers

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

Regards,

Sergey

Cheers Carsten

Cheers

Sergey