SAP ABAP 日期相关函数

获的两个日期之间的分钟数

data min TYPE i.

CALL FUNCTION 'DELTA_TIME_DAY_HOUR'
  EXPORTING
    T1            = '060000'
    T2            = '070000'
    D1            = '20160101'
    D2            = '20160101'
 IMPORTING
   MINUTES        = min          .

write min.

 获取两个日期期间的:天数、周数、月数、年数

包括期间月份的开始(月份第一天日期)、截止日期(月份最后一天日期)

TYPE-POOLS: p99sg.
DATA: days   TYPE i,
      weeks  TYPE i,
      months TYPE i,
      years  TYPE i.
 
DATA: month_tab TYPE STANDARD TABLE OF p99sg_month_tab_row.
 
CALL FUNCTION 'HR_99S_INTERVAL_BETWEEN_DATES'
  EXPORTING
    begda     = '20150101'    "开始日期
    endda     = '20160101'    "截止日期
  IMPORTING
    days      = days          "返回天数:266
    c_weeks   = weeks         "返回周数:52
    c_months  = months        "返回月数:12
    c_years   = years         "返回年数:1
    month_tab = month_tab.    "期间月份首尾日
 

过函数计算两个日期的差异月份

      CALL FUNCTION 'MONTHS_BETWEEN_TWO_DATES_NEW'
        EXPORTING
          I_DATUM_BIS             = 
          I_DATUM_VON             = 
*       I_KZ_INCL_BIS           = ' '
*       I_KZ_VOLLE_MONATE       = 'X'
       IMPORTING
         E_MONATE                = 

获取输入日期月份的最后一天的日期

CALL FUNCTION 'LAST_DAY_OF_MONTHS' EXPORTING day_in = '20160104' "输入日期 IMPORTING last_day_of_month = "返回最后一天日期: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 = date           20160131
  EXCEPTIONS
    day_in_no_date    = 1
    OTHERS            = 2.

 

CALL FUNCTION 'BKK_GET_MONTH_LASTDAY'
  EXPORTING
    i_date = '20160101'   "输入日期
  IMPORTING
    e_date = date. 20160131

根据条件获取需要的日期 

CALL FUNCTION 'RP_CALC_DATE_IN_INTERVAL'
  EXPORTING
    date      = '20160101'    "日期
    days      = 10            "天数
    months    = 0             "月数
    signum    = '+'           
    years     = 0             "年数
  IMPORTING
    calc_date = calc_date.    (2016.01.11
CALL FUNCTION 'FIMA_DATE_CREATE'
  EXPORTING
    I_DATE                        = '20160101'
   I_FLG_END_OF_MONTH            = ''
   I_YEARS                       = 1
*   I_MONTHS                      = 0
   I_DAYS                        = 1
*   I_CALENDAR_DAYS               = 2
  " I_SET_LAST_DAY_OF_MONTH       = 'X'
 IMPORTING
   E_DATE                        = w_date
   E_FLG_END_OF_MONTH            = w_flag
*   E_DAYS_OF_I_DATE              = w_days
          .
write:  w_date,
      / w_flag,
      / w_days.

判断是否为假日

CALL FUNCTION 'HOLIDAY_CHECK_AND_GET_INFO'
  EXPORTING
    date                         = '20160101'  "输入日期:春节
    holiday_calendar_id          = ''        "假日日历 ID
    with_holiday_attributes      = ' '         "属性标识
  IMPORTING
    holiday_found                = www     "输出是否为假日的标记  输出结果:X
  TABLES
    holiday_attributes           = attributes  "输出该假日的属性
                                               "若WITH_HOLIDAY_ATTRIBUTES参数 = X,则会带出属性记录
  EXCEPTIONS
    calendar_buffer_not_loadable = 1
    date_after_range             = 2
    date_before_range            = 3
    date_invalid                 = 4
    holiday_calendar_id_missing  = 5
    holiday_calendar_not_found   = 6
    OTHERS                       = 7.

转换日期为内部数字格式

CALL FUNCTION 'CONVERT_DATE_TO_INTERNAL'
  EXPORTING
    date_external            = '2016.01.01' "当前用户日期格式:YYYY.MM.DD
    accept_initial_date      = ' '
  IMPORTING
    date_internal            = date         "输出20160101
  EXCEPTIONS
    date_external_is_invalid = 1
    OTHERS                   = 2.

转换日期为系统日期格式

CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL'
  EXPORTING
    date_internal            = '20160101'  
  IMPORTING
    date_external            =                        2016.01.01
  EXCEPTIONS
    date_internal_is_invalid = 1
    OTHERS                   = 2.

查看日期属性

DATA: year_of_valid_from TYPE scal-year,
      year_of_valid_to   TYPE scal-year,
      return_code        TYPE sy-subrc.
 
DATA day_attributes TYPE STANDARD TABLE OF casdayattr.
 
CALL FUNCTION 'DAY_ATTRIBUTES_GET'
  EXPORTING
    factory_calendar           = ' '              "工厂日历ID
    holiday_calendar           = ' '              "假期日历ID
    date_from                  = '20140101'       "开始日期
    date_to                    = '20140110'       "截止日期
    language                   = sy-langu         "系统当前语言
    non_iso                    = ' '
  IMPORTING
    year_of_valid_from         = year_of_valid_from
    year_of_valid_to           = year_of_valid_to
    returncode                 = return_code
  TABLES
    day_attributes             = day_attributes "返回日期每日属性
  EXCEPTIONS
    factory_calendar_not_found = 1
    holiday_calendar_not_found = 2
    date_has_invalid_format    = 3
    date_inconsistency         = 4
    OTHERS                     = 5.
 

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.

SAP ABAP 日期相关函数_第1张图片

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.

SAP ABAP 日期相关函数_第2张图片

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.

SAP ABAP 日期相关函数_第3张图片

弹出选择周的对话框

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.  "返回选择周最后一天

SAP ABAP 日期相关函数_第4张图片

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.
 
WRITE:/ lv_in_time . 
WRITE:/ lv_out_time. 

08:00:00
20:00:00

 

你可能感兴趣的:(SAP ABAP 日期相关函数)