ABAP: How to add days to a date of type TIMESTAMP

15,659

Solution 1

Here's a solution that should work for you. The function module that should be available in your system is TIMESTAMP_DURATION_ADD.

REPORT zzz.

DATA lv_time TYPE timestamp VALUE '20180228000000'.

START-OF-SELECTION.
  DATA timestamp_out TYPE timestamp.

  CALL FUNCTION 'TIMESTAMP_DURATION_ADD'
    EXPORTING
      timestamp_in    = lv_time
      timezone        = 'UTC'
      duration        = 1
      unit            = 'TAG' " day (in German)
    IMPORTING
      timestamp_out   = timestamp_out
    EXCEPTIONS
      timestamp_error = 1
      OTHERS          = 2.

  ASSERT sy-subrc = 0.

  WRITE timestamp_out.

Solution 2

After looking at Jagger's answer, I looked under the hood of the TIMESTAMP_DURATION_ADD function and discovered some ABAP syntax that does the job without needing a function call at all.

constants: lc_time_zone type timezone value 'UTC'.
data lv_timestamp_before type timestamp value '20180228001234'.
data lv_timestamp_after type timestamp.
data lv_date like sy-datum.
data lv_time like sy-uzeit.

convert time stamp lv_timestamp_before time zone lc_time_zone
    into date lv_date time lv_time.
lv_date = lv_date + 1.
convert date lv_date time lv_time
    into time stamp lv_timestamp_after time zone lc_time_zone.

Solution 3

If you're not into resurrecting old procedural programming, you could use the class CL_ABAP_TSTMP as described in the documentation.

DATA some_timestamp TYPE timestamp VALUE '20180228000000'.
DATA(new_timestamp) = cl_abap_tstmp=>add(
    tstmp = some_timestamp
    secs  = ( 365 * 24 * 60 * 60  )
).

(Too bad CL_ABAP_TSTMP=>SECSOFDAY is private... But hey, at least that makes you think about what to do with leap years...)

Share:
15,659

Related videos on Youtube

Jonathan Benn
Author by

Jonathan Benn

Biography: I learned to program Basic when I was 12 years old. This started a fascination with computers and technology that has never faded. I love making computing technology more accessible and useful to people Expertise: Software Engineering, Automated Testing, User-Interface Design, Teaching, Design Thinking

Updated on May 25, 2022

Comments

  • Jonathan Benn
    Jonathan Benn almost 2 years

    In the ABAP programming language, how can I add 1 day to a time of type TIMESTAMP and have the month/year roll over correctly?

    For example:

    data lv_time type TIMESTAMP value '20180228000000'.
    data(lv_new_time) = lv_time + 1. " should be '20180301000000', but instead is '20180228000001'
    data(lv_new_time2) = lv_time + 1000000. " should be '20180301000000', but instead is '20180229000000'
    
    • Jagger
      Jagger about 6 years
      In SAP the TIMESTAMP type is simply a floating point type therefore the results you get are correct. However there is a way to do it in a neat way. I remember I did it some time ago but I have to browse through my resources. Will let you know.
  • Jonathan Benn
    Jonathan Benn about 6 years
    The above code will dump at stamp_date = stamp(8) with the message Overflow during an arithmetic operation (type P). I know it's weird, since the DATETIME is really a string one would expect string manipulation functions to work on it. What Function Module would you recommend? Can you give an example of using it in this scenario? Thanks!
  • Jonathan Benn
    Jonathan Benn about 6 years
    Dumps with the error message Function module "IAM_TIMESTAMP_CALC" not found
  • VXLozano
    VXLozano about 6 years
    For Pete's Sake, which SAP version are you running? that FM exists since 2005 (although is not released)
  • VXLozano
    VXLozano about 6 years
    I do not provide full working code, just hints. Check Joszef answer, it works.
  • Jonathan Benn
    Jonathan Benn about 6 years
    The function module IAM_TIMESTAMP_CALC doesn't exist on the S/4HANA Infinity System (software component S4CORE), nor on the Suite on HANA Infinity System (software component SAP_APPL). Hence, it's definitely not a standard function.
  • Jagger
    Jagger about 6 years
    I still think calling a function module is a better idea. :)
  • Jonathan Benn
    Jonathan Benn about 6 years
    @Jagger, how come? :) Personally, I like the idea of using basic ABAP syntax. It takes only 3 lines of code and is more system-independent.
  • Sandra Rossi
    Sandra Rossi about 6 years
    General rule of thumb : SAP says to not use "unreleased" function modules (i.e. one day, SAP could remove it without warning or mark it obsolete). CONVERT is released and documented. And CONVERT is much faster.
  • Jagger
    Jagger about 6 years
    @SandraRossi On the other hand, the general rule of thumb has that you should not duplicate the code. :)
  • Sandra Rossi
    Sandra Rossi about 6 years
    @Jagger That's not a "duplicate" because it's only 3 ABAP statements - could even simplify it to remove the 2 CONVERT, and do only assignments, no need to be inspired by some SAP code :) data(tstmp) = value tzntimestp( lv_timestamp ). ADD 1 TO tstmp(8). lv_timestamp = tstmp. but well, I don't like it a lot, so no solution is perfect ! (essentially because SAP does not provide a kernel-based date/time library)
  • Jonathan Benn
    Jonathan Benn about 6 years
    Can you please give a working code example for our readers? :)
  • vwegert
    vwegert about 6 years
    This is not even a sensible approach. Please remove this "answer" and stop leading other people in search of answers up the garden path.
  • VXLozano
    VXLozano about 6 years
    Up to now, the most voted answer uses that non-sensible approach, just with a proper working code snippet. Maybe you are right about my answer, but as I'm pretty new here, and I'm not sure about it, feel free to flag it and let the mods delete it if proceed. I'll not complaint about it. I will just let people to sink my answer by raising the more appropiate ones, if you don't care.