当前位置: 代码迷 >> 综合 >> SAP ABAP 日期、时间相关函数
  详细解决方案

SAP ABAP 日期、时间相关函数

热度:83   发布时间:2023-12-11 22:01:38.0

SAP ABAP 日期,时间 相关函数

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

data min TYPE i.CALL FUNCTION 'DELTA_TIME_DAY_HOUR'EXPORTINGT1            = '060000'T2            = '070000'D1            = '20160101'D2            = '20160101'IMPORTINGMINUTES        = 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'EXPORTINGbegda     = '20150101'    "开始日期endda     = '20160101'    "截止日期IMPORTINGdays      = days          "返回天数:266c_weeks   = weeks         "返回周数:52c_months  = months        "返回月数:12c_years   = years         "返回年数:1month_tab = month_tab.    "期间月份首尾日

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

      CALL FUNCTION 'MONTHS_BETWEEN_TWO_DATES_NEW'EXPORTINGI_DATUM_BIS             = I_DATUM_VON             = 
*       I_KZ_INCL_BIS           = ' '
*       I_KZ_VOLLE_MONATE       = 'X'IMPORTINGE_MONATE                = 

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

CALL FUNCTION 'LAST_DAY_OF_MONTHS'EXPORTINGday_in            = '20160104'     "输入日期IMPORTINGlast_day_of_month =            "返回最后一天日期:20160131EXCEPTIONSday_in_no_date    = 1OTHERS            = 2.

CALL FUNCTION 'RP_LAST_DAY_OF_MONTHS'EXPORTINGday_in            = '20160101'    IMPORTINGlast_day_of_month = date           20160131EXCEPTIONSday_in_no_date    = 1OTHERS            = 2.

CALL FUNCTION 'BKK_GET_MONTH_LASTDAY'EXPORTINGi_date = '20160101'   "输入日期IMPORTINGe_date = date. 20160131

根据条件获取需要的日期 

CALL FUNCTION 'RP_CALC_DATE_IN_INTERVAL'EXPORTINGdate      = '20160101'    "日期days      = 10            "天数months    = 0             "月数signum    = '+'           years     = 0             "年数IMPORTINGcalc_date = calc_date.    (2016.01.11)
ALL FUNCTION 'FIMA_DATE_CREATE'EXPORTINGI_DATE                        = '20160101'I_FLG_END_OF_MONTH            = ''I_YEARS                       = 1
*   I_MONTHS                      = 0I_DAYS                        = 1
*   I_CALENDAR_DAYS               = 2" I_SET_LAST_DAY_OF_MONTH       = 'X'IMPORTINGE_DATE                        = w_dateE_FLG_END_OF_MONTH            = w_flag
*   E_DAYS_OF_I_DATE              = w_days.
write:  w_date,/ w_flag,/ w_days.

DATA date TYPE sy-datum.CALL FUNCTION 'MONTH_PLUS_DETERMINE'EXPORTINGmonths  = 3olddate = '20140101'   "输入日期IMPORTINGnewdate = date.        "返回日期:20140401
DATA date TYPE sy-datum.CALL FUNCTION 'CCM_GO_BACK_MONTHS'EXPORTINGcurrdate   = '20140101'   "输入日期backmonths = 3            "过去月数IMPORTINGnewdate    = date.        "返回日期:20131001

判断是否为假日

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 'HOLIDAY_CHECK_AND_GET_INFO'EXPORTINGdate                = <lfs_tab>-datumholiday_calendar_id = 'CN'IMPORTINGholiday_found       = v_cind.IF v_cind = ''.CALL FUNCTION 'DAY_IN_WEEK'EXPORTINGdatum = <lfs_tab>-datumIMPORTINGwotnr = v_wotnr.CASE v_wotnr.WHEN 6 OR 7.v_cind = 'X'.ENDCASE.ENDIF.

转换日期为内部数字格式

CALL FUNCTION 'CONVERT_DATE_TO_INTERNAL'EXPORTINGdate_external            = '2016.01.01' "当前用户日期格式:YYYY.MM.DDaccept_initial_date      = ' 'IMPORTINGdate_internal            = date         "输出20160101EXCEPTIONSdate_external_is_invalid = 1OTHERS                   = 2.

转换日期为系统日期格式

CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL'EXPORTINGdate_internal            = '20160101'  IMPORTINGdate_external            =                        2016.01.01EXCEPTIONSdate_internal_is_invalid = 1OTHERS                   = 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'EXPORTINGfactory_calendar           = ' '              "工厂日历IDholiday_calendar           = ' '              "假期日历IDdate_from                  = '20140101'       "开始日期date_to                    = '20140110'       "截止日期language                   = sy-langu         "系统当前语言non_iso                    = ' 'IMPORTINGyear_of_valid_from         = year_of_valid_fromyear_of_valid_to           = year_of_valid_toreturncode                 = return_codeTABLESday_attributes             = day_attributes "返回日期每日属性EXCEPTIONSfactory_calendar_not_found = 1holiday_calendar_not_found = 2date_has_invalid_format    = 3date_inconsistency         = 4OTHERS                     = 5.

*获取当前日期是星期几

DATA wotnr TYPE p.CALL FUNCTION 'DAY_IN_WEEK'EXPORTINGdatum = '20140101'  "输入日期IMPORTINGwotnr = wotnr.      "返回:3 =》星期三/周三

*获取当前日期周周一周日的日期

  CALL FUNCTION 'GET_WEEK_INFO_BASED_ON_DATE'EXPORTINGdate   = sy-datum   "'20140101'IMPORTINGweek   = week       "返回周数:201401monday = monday     "返回周一日期:2013.12.30sunday = sunday.    "返回周日日期:2014.01.05

DATA date TYPE sy-datum.CALL FUNCTION 'WEEK_GET_FIRST_DAY'EXPORTINGweek         = '201401'   "输入周IMPORTINGdate         = date       "返回该周第一天(周一):2013.12.30EXCEPTIONSweek_invalid = 1OTHERS       = 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'EXPORTINGdate_for_first_month         = sy-datumIMPORTINGselect_date                  = l_date     "用户选择后返回的日期EXCEPTIONScalendar_buffer_not_loadable = 1date_after_range             = 2date_before_range            = 3date_invalid                 = 4factory_calendar_not_found   = 5holiday_calendar_not_found   = 6parameter_conflict           = 7OTHERS                       = 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'EXPORTINGstart_time    = sy-uzeitdisplay       = ' 'IMPORTINGselected_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'EXPORTINGactual_month               = actual_month   "传入年份factory_calendar           = ' '            "工厂日历 IDholiday_calendar           = ' '            "假日日历 IDlanguage                   = sy-langu       "当前系统语言start_column               = 8              "弹出框屏幕位置start_row                  = 5              "弹出框屏幕位置IMPORTINGselected_month             = selected_month "返回用户输入月份return_code                = return_code    "返回 sy-subrcEXCEPTIONSfactory_calendar_not_found = 1holiday_calendar_not_found = 2month_not_found            = 3OTHERS                     = 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'EXPORTINGsel_day      = 'X'        "启用天 选择sel_week     = 'X'        "启用周 选择sel_month    = 'X'        "启用月 选择sel_interval = 'X'focus_day    = sy-datum   "默认选择日期IMPORTINGbegin_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'EXPORTINGtype_time       = 'B'    " A = 24小时制 -> 12小时制  B = 12小时制 -> 24小时制input_time      = lv_in_timeinput_am_pm     = 'PM'IMPORTINGoutput_time     = lv_out_timeoutput_am_pm    = lv_am_pmEXCEPTIONSparameter_error = 1OTHERS          = 2.WRITE:/ lv_in_time . 
WRITE:/ lv_out_time. 

08:00:00
20:00:00

工作计划

HR_ABS_ATT_TIMES_AT_ENTRY

HR_PERSON_READ_WORK_SCHEDULE

日期格式转换

CALL FUNCTION 'FORMAT_DATE_4_OUTPUT'EXPORTINGDATIN         = FORMAT        =
* IMPORTING
*   DATEX         =.

DATA date2(10) TYPE c.CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL'EXPORTINGdate_internal            = '20140101'  IMPORTINGdate_external            = date2     "根据用户日期格式参数输出:YYYY.MM.DDEXCEPTIONSdate_internal_is_invalid = 1OTHERS                   = 2.

时间戳

长度 15的 DEC类型,组成为  8位日期 + 6位时间。

 把时间和日期转成时间戳

CALL FUNCTION 'CIF_GEN4_CONVERT_DATETIME'   EXPORTINGiv_date                      = l_datum1IV_TIME                      = l_time1IV_TIMEZONE                  = SY-ZONLOIMPORTINGEV_TIMESTAMP                 = l_timestamp1EXCEPTIONSTIME_CONVERSION_FAILED       = 1OTHERS                       = 2. 
CALL FUNCTION 'IB_CONVERT_INTO_TIMESTAMP'EXPORTINGI_DATLO           =I_TIMLO           =
*   I_TZONE           = SY-ZONLO
* IMPORTING
*   E_TIMESTAMP       =.

REPORT  z_barry_test.TABLES: t001w,adrc .
DATA: date LIKE sy-datum,time LIKE sy-uzeit,tstamp LIKE tzonref-tstamps.SELECT SINGLE * FROM t001w WHERE werks = '0001'.
IF sy-subrc = 0.SELECT SINGLE * FROM adrc WHERE addrnumber = t001w-adrnr . "time_zonePERFORM date_to_timestamp USING '20080808' '080808' adrc-time_zoneCHANGING tstamp.WRITE tstamp.
ENDIF.
IF NOT tstamp IS INITIAL.PERFORM timestamp_to_date USING tstamp adrc-time_zoneCHANGING date time.WRITE / : date,time .
ENDIF.*&---------------------------------------------------------------------*
*&      Form  date_to_timestamp
*&---------------------------------------------------------------------*
*       日期+时间+时区  转变为 时间戳
*----------------------------------------------------------------------*
FORM date_to_timestamp USING i_date LIKE sy-datumi_time LIKE sy-uzeiti_tzone LIKE  tzonref-tzoneCHANGING o_tstamp LIKE tzonref-tstamps  .CONVERT DATE i_date TIME i_time INTO TIME STAMP o_tstamp TIME ZONE i_tzone.CASE sy-subrc.WHEN 8.MESSAGE e000(oo) WITH '时区错'.WHEN 12.MESSAGE e000(oo) WITH '时间戳错误'.WHEN OTHERS.ENDCASE.
ENDFORM.                    "date_to_timestamp*&---------------------------------------------------------------------*
*&      Form  TIMESTAMP_TO_DATE
*&---------------------------------------------------------------------*
*       时间戳+时区 转变为 日期和时间
*----------------------------------------------------------------------*
FORM timestamp_to_date USING   i_tstamp LIKE tzonref-tstampsi_tzone LIKE  tzonref-tzoneCHANGING o_date LIKE  sy-datumo_time LIKE  sy-uzeit.DATA: w_tzone LIKE tzonref-tzone.w_tzone = i_tzone.CONVERT TIME STAMP i_tstamp TIME ZONE w_tzone INTO DATE o_date TIME o_time.CASE sy-subrc.WHEN 8.MESSAGE e000(oo) WITH '时区错'.WHEN 12.MESSAGE e000(oo) WITH '时间戳错误'.WHEN OTHERS.ENDCASE.
ENDFORM.                    "TIMESTAMP_TO_DATE

把时间戳转成时间和日期

CALL FUNCTION 'CIF_GEN4_CONVERT_TIMESTAMP'EXPORTINGiv_timestamp                = l_timestamp3IV_TIMEZONE                  = SY-ZONLOIMPORTINGEV_DATE                      = l_datum3EV_TIME                      = l_time3EXCEPTIONSTIME_CONVERSION_FAILED       = 1OTHERS                       = 2.