首页 > 代码库 > 计算两个日期之间的工作日

计算两个日期之间的工作日

兰大顾的。。。

FUNCTION zget_workdays.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(I_DATE1) LIKE  SY-DATUM
*"     VALUE(I_DATE2) LIKE  SY-DATUM
*"  EXPORTING
*"     VALUE(E_DAYS) TYPE  I
*"----------------------------------------------------------------------
  DATA: week1   TYPE scal-week,
        monday1 TYPE sy-datum,
        sunday1 TYPE sy-datum,
        week2   TYPE scal-week,
        monday2 TYPE sy-datum,
        sunday2 TYPE sy-datum,
        days1    TYPE i,
        days2    TYPE i,
        wotnr1   TYPE p,
        wotnr2   TYPE p.

  CHECK i_date2 GE i_date1.

  CALL FUNCTION DAY_IN_WEEK
    EXPORTING
      datum = i_date1
    IMPORTING
      wotnr = wotnr1.

  CALL FUNCTION DAY_IN_WEEK
    EXPORTING
      datum = i_date2
    IMPORTING
      wotnr = wotnr2.

  "计算开始日期所在周
  CALL FUNCTION GET_WEEK_INFO_BASED_ON_DATE
    EXPORTING
      date   = i_date1
    IMPORTING
      week   = week1
      monday = monday1
      sunday = sunday1.

  "计算结束日期所在周
  CALL FUNCTION GET_WEEK_INFO_BASED_ON_DATE
    EXPORTING
      date   = i_date2
    IMPORTING
      week   = week2
      monday = monday2
      sunday = sunday2.

  IF week1 EQ week2.
    IF wotnr1 GE 5.
      e_days = 0.
      EXIT.
    ELSE.
      IF wotnr2 LE 5.
        e_days = i_date2 - i_date1 .
      ELSEIF wotnr2 EQ 6.
        e_days = i_date2 - i_date1 - 1.
      ELSEIF wotnr2 EQ 7.
        e_days = i_date2 - i_date1 - 2.
      ENDIF.
    ENDIF.
  ELSE.
    IF wotnr1 gt 5.
      days1 = 0.
    ELSE.
      days1 = sunday1 - 2 - i_date1 + 1.
    ENDIF.

    IF wotnr2 GT 5 .
      days2 = 4.
    ELSE.
      days2 = i_date2 - monday2.
    ENDIF.

    e_days = ( week2 - week1 - 1 ) * 5 + days1 + days2.

  ENDIF.


ENDFUNCTION.

 

计算两个日期之间的工作日