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'
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.
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...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With