源码
FUNCTION zfm_date_difference.
*“----------------------------------------------------------------------
"“本地接口:
*” IMPORTING
*” VALUE(IV_DATE_BEG) TYPE SY-DATUM
*" VALUE(IV_TIME_BEG) TYPE SY-UZEIT
*" VALUE(IV_DATE_END) TYPE SY-DATUM
*" VALUE(IV_TIME_END) TYPE SY-UZEIT
*" EXPORTING
*" VALUE(EV_MESSAGE) TYPE CHAR100
*"----------------------------------------------------------------------
DATA: lv_timediff TYPE t,
lv_date_b TYPE d,
lv_date_s TYPE d,
lv_time_b TYPE t,
lv_time_s TYPE t.
DATA: lv_diff_d TYPE p, "天数差
lv_diff_h TYPE p, "小时数差
lv_diff_m TYPE p, "分钟数差
lv_diff_s TYPE p, "秒数差
lv_diff_s_all TYPE p, "总秒数差
lv_diff_s_exh TYPE p. "除去小时秒数的秒数
*–1.计算天数差使用函数’SD_DATETIME_DIFFERENCE’有误(20210507 230000 - 20210507 000000)
IF iv_date_beg = iv_date_end.
lv_diff_d = 0.
IF iv_time_end > iv_time_beg.
lv_time_b = iv_time_end.
lv_time_s = iv_time_beg.
ELSE.
lv_time_b = iv_time_beg.
lv_time_s = iv_time_end.
ENDIF.
ELSEIF iv_date_beg > iv_date_end.
lv_date_b = iv_date_beg.
lv_date_s = iv_date_end.
lv_time_b = iv_time_beg.
lv_time_s = iv_time_end.
ELSEIF iv_date_beg < iv_date_end.
lv_date_b = iv_date_end.
lv_date_s = iv_date_beg.
lv_time_b = iv_time_end.
lv_time_s = iv_time_beg.
ENDIF.
lv_timediff = lv_time_b - lv_time_s.
IF lv_timediff > lv_time_b AND iv_date_beg <> iv_date_end.
lv_diff_d = lv_date_b - lv_date_s - 1.
ELSEIF lv_timediff = lv_time_b AND iv_date_beg <> iv_date_end.
lv_diff_d = lv_date_b - lv_date_s .
ELSEIF lv_timediff < lv_time_b AND iv_date_beg <> iv_date_end.
lv_diff_d = lv_date_b - lv_date_s .
ENDIF.
lv_diff_s_all = lv_time_b+0(2) * 3600 + lv_time_b+2(2) * 60 + lv_time_b+4(2) -
lv_time_s+0(2) * 3600 - lv_time_s+2(2) * 60 - lv_time_s+4(2) .
lv_diff_h = floor( abs( lv_diff_s_all ) / 3600 ).
lv_diff_s_exh = abs( abs( lv_diff_s_all ) - abs( lv_diff_h * 3600 ) ).
lv_diff_m = floor( lv_diff_s_exh / 60 ) .
lv_diff_s = abs( lv_diff_s_exh - lv_diff_m * 60 ).
IF lv_diff_s_all < 0.
lv_diff_h = 23 - lv_diff_h.
lv_diff_m = 59 - lv_diff_m.
lv_diff_s = 60 - lv_diff_s.
ENDIF.
ev_message = lv_diff_d && ‘天’ && lv_diff_h && ‘时’ &&
lv_diff_m && ‘分’ && lv_diff_s && ‘秒’.
ENDFUNCTION.