ABAP log

January 24, 2007

Simple ABAP macro to calculate the last day of month.

Filed under: ABAP, SAP — abaplog @ 8:36 pm

One of common questions on SAP-related forums is how to get the last day of given month. While there are many functions that you can use, why not to define a simple macro?

* Macro to get the last day of month
DEFINE TO_LAST_DAY.
  &1+6(2) = '01'.
  &1 = &1 + 31.
  &1+6(2) = '01'.
  &1 = &1 - 1.
END-OF-DEFINITION.

Use it like that:

data:
  lf_date type d.

lf_date = sy-datum.
to_last_day lf_date.
Advertisements

2 Comments »

  1. Hi!

    This tip won’t always work correctly.
    Let’s assume sy-datum = ‘20090131’. In this case incrementing the date with 31 days would result in ‘20090303’ which will eventually result in ‘20090228’ as the last day of January. This is obviously incorrect.

    MikKi

    Comment by MikKi — May 25, 2009 @ 12:39 pm

    • MikKi, the very first line in the macro is to move the current day to the 1st of the month. So it will move from 0131 to 0101, then to 0201, then back to 0131. Try it yourself!

      Comment by abaplog — June 4, 2009 @ 1:57 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: