FUNCTION ZGET_DATE_DIFF.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" REFERENCE(DATE1) TYPE D
*" REFERENCE(TIME1) TYPE T
*" REFERENCE(DATE2) TYPE D
*" REFERENCE(TIME2) TYPE T
*" EXPORTING
*" REFERENCE(TIMELIST) TYPE TXT40
*" REFERENCE(ARRBEG) TYPE TXT20
*" REFERENCE(ARREND) TYPE TXT20
*" EXCEPTIONS
*" INVALID_DATETIME
*"----------------------------------------------------------------------
DATA: T_DATEDIFF TYPE P,
T_TIMEDIFF TYPE P,
T_EARLIEST TYPE C,
T_E_TDIFF LIKE TVRO-FAHZTD,
T_E_DATE2_EARLY LIKE RV56A-SELKZ.
DATA: T_CHAR1 LIKE RV56A-VHART, "天
T_CHAR2 LIKE RV56A-TRSTACT, "小时
T_CHAR3 LIKE RV56A-TRG_AMPEL. "分钟
DATA: T_CHAR4 LIKE TVRO-TDLNR, " 临时存放日期
T_CHAR5 LIKE TVRO-TDLNR. " 临时存放时间
IFDATE1 IS INITIAL OR TIME1 IS INITIAL ORDATE2 IS INITIAL AND TIME2 IS INITIAL.
MESSAGE '数据中日期时间存在为空的问题,请更正数据!' TYPE'I'.
"RETURN.
ENDIF.
CALL FUNCTION 'SD_DATETIME_DIFFERENCE'
EXPORTING
DATE1 = DATE1
TIME1 = TIME1
DATE2 = DATE2
TIME2 = TIME2
IMPORTING
DATEDIFF =T_DATEDIFF " 3 天
TIMEDIFF =T_TIMEDIFF " 10 小时
EARLIEST =T_EARLIEST " 1(DATE2>=DATE1) $ 2(DATE2<DATE1)
EXCEPTIONS
INVALID_DATETIME = 1
OTHERS = 2
.
IFSY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
CALL FUNCTION 'SD_CALC_DURATION_FROM_DATETIME'
EXPORTING
I_DATE1 = DATE1
I_TIME1 = TIME1
I_DATE2 = DATE2
I_TIME2 = TIME2
IMPORTING
E_TDIFF =T_E_TDIFF " 13:40 13 小时 40 分钟
E_DATE2_EARLY =T_E_DATE2_EARLY " X 或 空
EXCEPTIONS
INVALID_DATETIME = 1
OTHERS = 2
.
IFSY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBERSY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
T_CHAR1 = T_DATEDIFF.
T_CHAR2 = T_TIMEDIFF.
T_CHAR3 = T_E_TDIFF.
T_CHAR3 = T_CHAR3+2(2).
" 如果分钟超出 30 分,则要将小时数减去 1,因为前边的 FM 计算如果超出 30 分,将小时数加 1.
IFT_CHAR3 > 30.
T_CHAR2 = T_CHAR2 - 1.
ENDIF.
IFT_CHAR1 > 0.
CONCATENATE T_CHAR1 '天' T_CHAR2 '小时' T_CHAR3 '分钟' INTO TIMELIST.
ELSE.
IF T_CHAR2 > 0 .
CONCATENATE T_CHAR2 '小时' T_CHAR3 '分钟' INTO TIMELIST.
ELSE.
IF T_CHAR3 IS INITIAL. " 如果分钟为 0 ,也就是时间用了几秒的话,则将时间设置为 1 分钟
T_CHAR3 = '1'.
ENDIF.
CONCATENATE T_CHAR3 '分钟' INTO TIMELIST.
ENDIF.
ENDIF.
IF DATE1 IS NOT INITIAL AND TIME1 IS NOT INITIAL AND DATE2 IS NOT INITIAL ANDTIME2 IS NOT INITIAL.
T_CHAR4 = DATE1.
T_CHAR5 = TIME1.
CONCATENATE T_CHAR4+0(4) '-' T_CHAR4+4(2) '-' T_CHAR4+6(2) ' ' T_CHAR5+0(2) ':' T_CHAR5+2(2) ':'T_CHAR5+4(2) INTO ARRBEG RESPECTINGBLANKS.
T_CHAR4 = DATE2.
T_CHAR5 = TIME2.
CONCATENATE T_CHAR4+0(4) '-' T_CHAR4+4(2) '-' T_CHAR4+6(2) ' ' T_CHAR5+0(2) ':' T_CHAR5+2(2) ':'T_CHAR5+4(2) INTO ARREND RESPECTINGBLANKS.
ENDIF.
ENDFUNCTION.