1. 程式人生 > 實用技巧 >ABAP 日期相關函式

ABAP 日期相關函式

SAP ABAP 日期,時間 相關函式

獲的兩個日期之間的分鐘數

DATA MIN TYPE I.
CALL FUNCTION 'DELTA_TIME_DAY_HOUR'
  EXPORTING
    T1      = '060000'
    T2      = '080000'
    D1      = '20160101'
    D2      = '20160101'
  IMPORTING
    MINUTES = MIN.	"相差120分鐘

獲取兩個日期期間的:天數、週數、月數、年數,包括期間月份的開始(月份第一天日期)、截止日期(月份最後一天日期)

TYPE-POOLS P99SG.
DATA: DAYS   TYPE I,
      WEEKS  TYPE I,
      MONTHS TYPE I,
      YEARS  TYPE I.
DATA: MONTH_TAB TYPE P99SG_MONTH_TAB.
CALL FUNCTION 'HR_99S_INTERVAL_BETWEEN_DATES'
  EXPORTING
    BEGDA     = '20150101'    "開始日期
    ENDDA     = '20160131'    "截止日期
    TAB_MODE  = 'I'           "包含月份首尾日
  IMPORTING
    DAYS      = DAYS          "返回天數:366
    C_WEEKS   = WEEKS         "返回週數:52
    C_MONTHS  = MONTHS        "返回月數:12
    C_YEARS   = YEARS         "返回年數:1
    MONTH_TAB = MONTH_TAB.    "期間月份首尾日

獲取輸入日期月份的最後一天的日期

DATA L_D TYPE SY-DATUM.
CALL FUNCTION 'LAST_DAY_OF_MONTHS'
  EXPORTING
    DAY_IN            = '20160104'     	"輸入日期
  IMPORTING
    LAST_DAY_OF_MONTH = L_D            	"返回最後一天日期:20160131
  EXCEPTIONS
    DAY_IN_NO_DATE    = 1
    OTHERS            = 2.
    
CALL FUNCTION 'RP_LAST_DAY_OF_MONTHS'
  EXPORTING
    DAY_IN            = '20160101'		"輸入日期
  IMPORTING
    LAST_DAY_OF_MONTH = L_D           	"返回最後一天日期:20160131
  EXCEPTIONS
    DAY_IN_NO_DATE    = 1
    OTHERS            = 2.
    
CALL FUNCTION 'BKK_GET_MONTH_LASTDAY'
  EXPORTING
    I_DATE = '20160101'   				"輸入日期
  IMPORTING
    E_DATE = L_D. 						"返回最後一天日期:20160131

根據條件獲取需要的日期

DATA L_D TYPE D.
DATA W_FLAG(1) TYPE C.
DATA W_DAYS(2) TYPE N.
CALL FUNCTION 'RP_CALC_DATE_IN_INTERVAL'
  EXPORTING
    DATE      = '20160101'    "日期
    DAYS      = 10            "天數
    MONTHS    = 1             "月數
    SIGNUM    = '+'
    YEARS     = 1             "年數
  IMPORTING
    CALC_DATE = L_D.          "2017.02.11
    
CALL FUNCTION 'FIMA_DATE_CREATE'
  EXPORTING
    I_DATE                  = '20160101'
    I_FLG_END_OF_MONTH      = 'X'
    I_YEARS                 = 1
    I_MONTHS                = 2
    I_DAYS                  = 1
*   I_CALENDAR_DAYS         = 1
    I_SET_LAST_DAY_OF_MONTH = 'X'
  IMPORTING
    E_DATE                  = L_D       "20170302
    E_FLG_END_OF_MONTH      = W_FLAG    "
    E_DAYS_OF_I_DATE        = W_DAYS.   "02 不知道有啥用    
    
CALL FUNCTION 'MONTH_PLUS_DETERMINE'
  EXPORTING
    MONTHS  = 3				"正數加,負數減
    OLDDATE = '20140101'    "輸入日期
  IMPORTING
    NEWDATE = L_D.          "返回日期:20140401    
    
CALL FUNCTION 'CCM_GO_BACK_MONTHS'
  EXPORTING
    CURRDATE   = '20140401'   "輸入日期
    BACKMONTHS = 3            "過去月數
  IMPORTING
    NEWDATE    = L_D.        "返回日期:20140101    

轉換日期為內部數字格式

DATA L_D(8) TYPE C.
CALL FUNCTION 'CONVERT_DATE_TO_INTERNAL'
  EXPORTING
    DATE_EXTERNAL            = '2016.01.01' "當前使用者日期格式:YYYY.MM.DD
    ACCEPT_INITIAL_DATE      = ''
  IMPORTING
    DATE_INTERNAL            = L_D         "輸出20160101
  EXCEPTIONS
    DATE_EXTERNAL_IS_INVALID = 1
    OTHERS                   = 2.

轉換日期為系統日期格式

DATA L_D TYPE STRING.
CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL'
  EXPORTING
    DATE_INTERNAL            = '20160101'
  IMPORTING
    DATE_EXTERNAL            = L_D              "2016.01.01
  EXCEPTIONS
    DATE_INTERNAL_IS_INVALID = 1
    OTHERS                   = 2.

獲取當前日期是星期幾

DATA WOTNR TYPE P.
CALL FUNCTION 'DAY_IN_WEEK'
  EXPORTING
    DATUM = '20201105'  "輸入日期
  IMPORTING
    WOTNR = WOTNR.      "返回:4 =》星期四/週四

獲取當前日期週週一週日的日期

DATA WEEK TYPE KWEEK.
DATA MONDAY TYPE D.
DATA SUNDAY TYPE D.
DATA DATE TYPE SY-DATUM.
CALL FUNCTION 'GET_WEEK_INFO_BASED_ON_DATE'
  EXPORTING
    DATE   = '20140101'
  IMPORTING
    WEEK   = WEEK       "返回週數:201401,不懂啥意思
    MONDAY = MONDAY     "返回週一日期:2013.12.30
    SUNDAY = SUNDAY.    "返回週日日期:2014.01.05
    
CALL FUNCTION 'WEEK_GET_FIRST_DAY'
  EXPORTING
    WEEK         = '201401'   "輸入周
  IMPORTING
    DATE         = DATE       "返回該周第一天(週一):2013.12.30
  EXCEPTIONS
    WEEK_INVALID = 1
    OTHERS       = 2.

F4_DATE 函式

PARAMETERS:P1(6) TYPE C.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P1.
  DATA:L_DATE TYPE SY-DATUM.
  CALL FUNCTION 'F4_DATE'
    EXPORTING
      DATE_FOR_FIRST_MONTH         = SY-DATUM
    IMPORTING
      SELECT_DATE                  = L_DATE     "使用者選擇後返回的日期
    EXCEPTIONS
      CALENDAR_BUFFER_NOT_LOADABLE = 1
      DATE_AFTER_RANGE             = 2
      DATE_BEFORE_RANGE            = 3
      DATE_INVALID                 = 4
      FACTORY_CALENDAR_NOT_FOUND   = 5
      HOLIDAY_CALENDAR_NOT_FOUND   = 6
      PARAMETER_CONFLICT           = 7
      OTHERS                       = 8.

F4_CLOCK 函式

PARAMETERS:P1(6) TYPE C.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P1.
  DATA: L_TIME TYPE SY-UZEIT.
  CALL FUNCTION 'F4_CLOCK'
    EXPORTING
      START_TIME    = SY-UZEIT
      DISPLAY       = ' '
    IMPORTING
      SELECTED_TIME = L_TIME.

F4 彈出框的形式選擇年月

PARAMETERS: P_YEAR  TYPE MARD-LFGJA DEFAULT SY-DATUM+0(4),
            P_MONTH TYPE MARD-LFMON.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_MONTH.
  DATA: ACTUAL_MONTH   LIKE ISELLIST-MONTH,
        SELECTED_MONTH LIKE ISELLIST-MONTH,
        RETURN_CODE    TYPE SY-SUBRC.
  CONCATENATE P_YEAR SY-DATUM+4(2) INTO ACTUAL_MONTH.
  CALL FUNCTION 'POPUP_TO_SELECT_MONTH'
    EXPORTING
      ACTUAL_MONTH               = ACTUAL_MONTH   "傳入年份
      FACTORY_CALENDAR           = ' '            "工廠日曆 ID
      HOLIDAY_CALENDAR           = ' '            "假日日曆 ID
      LANGUAGE                   = SY-LANGU       "當前系統語言
      START_COLUMN               = 8              "彈出框螢幕位置
      START_ROW                  = 5              "彈出框螢幕位置
    IMPORTING
      SELECTED_MONTH             = SELECTED_MONTH "返回使用者輸入月份
      RETURN_CODE                = RETURN_CODE    "返回 sy-subrc
    EXCEPTIONS
      FACTORY_CALENDAR_NOT_FOUND = 1
      HOLIDAY_CALENDAR_NOT_FOUND = 2
      MONTH_NOT_FOUND            = 3
      OTHERS                     = 4.
  IF RETURN_CODE = 0.
    P_YEAR  = SELECTED_MONTH+0(4).
    P_MONTH = SELECTED_MONTH+4(2).
  ENDIF.

彈出選擇周的對話方塊

DATA: BEGIN_DATE TYPE SY-DATUM,
      END_DATE   TYPE SY-DATUM.
CALL FUNCTION 'POPUP_CALENDAR_SDB'
  EXPORTING
    SEL_DAY      = 'X'        "啟用天 選擇
    SEL_WEEK     = 'X'        "啟用周 選擇
    SEL_MONTH    = 'X'        "啟用月 選擇
    SEL_INTERVAL = 'X'
    FOCUS_DAY    = SY-DATUM   "預設選擇日期
  IMPORTING
    BEGIN_DATE   = BEGIN_DATE "返回選擇周第一天
    END_DATE     = END_DATE.  "返回選擇周最後一天

12小時制與24小時制的時間轉換

DATA: LV_IN_TIME  TYPE TIMS,
      LV_OUT_TIME TYPE TIMS,
      LV_AM_PM    TYPE C.
LV_IN_TIME = '080000'.
CALL FUNCTION 'HRVE_CONVERT_TIME'
  EXPORTING
    TYPE_TIME       = 'B'    " A = 24小時制 -> 12小時制  B = 12小時制 -> 24小時制
    INPUT_TIME      = LV_IN_TIME
    INPUT_AM_PM     = 'PM'
  IMPORTING
    OUTPUT_TIME     = LV_OUT_TIME
    OUTPUT_AM_PM    = LV_AM_PM
  EXCEPTIONS
    PARAMETER_ERROR = 1
    OTHERS          = 2.