Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add days to a TIMESTAMP?

Tags:

datetime

abap

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'
like image 926
Jonathan Benn Avatar asked Aug 31 '25 17:08

Jonathan Benn


2 Answers

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.
like image 55
Jonathan Benn Avatar answered Sep 03 '25 09:09

Jonathan Benn


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

like image 32
vwegert Avatar answered Sep 03 '25 07:09

vwegert