1.计算两个时间的时间差:cl_abap_tstmp=>subtract
DATA: tstmp1 TYPE timestampl,
tstmp2 TYPE timestampl,
diff TYPE tzntstmpl. " P代表秒
" 获取两个时间戳
tstmp1 = '20230911183000'.
tstmp2 = '20230911153000'.
diff = cl_abap_tstmp=>subtract(
EXPORTING
tstmp1 = tstmp1
tstmp2 = tstmp2 ).
IF diff > 86000.
WRITE: '大于24 小时'.
ELSE.
WRITE: '小于24 小时'.
ENDIF.
WRITE: / diff.
其中 diff 是带符号的,如果 tstmp1 > tstmp2 , 则diff 是正数,反之负数
2.倒推过去的日期时间 :START_TIME_DETERMINE
PARAMETERS: p_inter TYPE sytabix OBLIGATORY.
CONSTANTS gc_msehi TYPE t006-msehi VALUE 'MIN'.
"Date fidle
DATA lv_start_date TYPE sy-datum.
DATA lv_start_time TYPE sy-uzeit.
DATA(lv_end_date) = p_datum.
DATA(lv_end_time) = p_uzeit.
" Get the start date and start time
CALL FUNCTION 'START_TIME_DETERMINE'
EXPORTING
duration = p_inter
unit = gc_msehi
IMPORTING
start_date = lv_start_date
start_time = lv_start_time
CHANGING
end_date = lv_end_date
end_time = lv_end_time
EXCEPTIONS
factory_calendar_not_found = 1
date_out_of_calendar_range = 2
date_not_valid = 3
unit_conversion_error = 4
si_unit_missing = 5
parameters_not_valid = 6
OTHERS = 7.
IF sy-subrc <> 0.
* Implement suitable error handling here
MESSAGE s022 DISPLAY LIKE gc_e.
RETURN .
ENDIF.
其中 p_inter 是整数的意思,unit 参数表示时间格式”时,分,秒“
如下图: