SAP ABAP 日期,时间 相关函数

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

data min TYPE i.

CALL FUNCTION 'DELTA_TIME_DAY_HOUR'
EXPORTING
T1 = ''
T2 = ''
D1 = ''
D2 = ''
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 = '' "开始日期
endda = '' "截止日期
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 = ''
IMPORTING
last_day_of_month = date
EXCEPTIONS
day_in_no_date =
OTHERS = .
CALL FUNCTION 'BKK_GET_MONTH_LASTDAY'
EXPORTING
i_date = '' "输入日期
IMPORTING
e_date = date. 20160131

根据条件获取需要的日期 

CALL FUNCTION 'RP_CALC_DATE_IN_INTERVAL'
EXPORTING
date = '' "日期
days = "天数
months = "月数
signum = '+'
years = "年数
IMPORTING
calc_date = calc_date. (2016.01.)
CALL FUNCTION 'FIMA_DATE_CREATE'
EXPORTING
I_DATE = ''
I_FLG_END_OF_MONTH = ''
I_YEARS =
* I_MONTHS = 0
I_DAYS =
* 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.
DATA date TYPE sy-datum.

CALL FUNCTION 'MONTH_PLUS_DETERMINE'
EXPORTING
months =
olddate = '' "输入日期
IMPORTING
newdate = date. "返回日期:20140401
DATA date TYPE sy-datum.

CALL FUNCTION 'CCM_GO_BACK_MONTHS'
EXPORTING
currdate = '' "输入日期
backmonths = "过去月数
IMPORTING
newdate = 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'
            EXPORTING
              date                = <lfs_tab>-datum
              holiday_calendar_id = 'CN'
            IMPORTING
              holiday_found       = v_cind.           IF v_cind = ''.
            CALL FUNCTION 'DAY_IN_WEEK'
              EXPORTING
                datum = <lfs_tab>-datum
              IMPORTING
                wotnr = v_wotnr.
            CASE v_wotnr.
              WHEN 6 OR 7.
                v_cind = 'X'.
            ENDCASE.
          ENDIF.

转换日期为内部数字格式

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 =
OTHERS = .

转换日期为系统日期格式

CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL'
EXPORTING
date_internal = ''
IMPORTING
date_external = 2016.01.
EXCEPTIONS
date_internal_is_invalid =
OTHERS = .

查看日期属性

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 = '' "开始日期
date_to = '' "截止日期
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 =
holiday_calendar_not_found =
date_has_invalid_format =
date_inconsistency =
OTHERS = .

*获取当前日期是星期几

DATA wotnr TYPE p.

CALL FUNCTION 'DAY_IN_WEEK'
EXPORTING
datum = '' "输入日期
IMPORTING
wotnr = wotnr. "返回:3 =》星期三/周三

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

  CALL FUNCTION 'GET_WEEK_INFO_BASED_ON_DATE'
EXPORTING
date = sy-datum "'20140101'
IMPORTING
week = week "返回周数:201401
monday = monday "返回周一日期:2013.12.30
sunday = sunday. "返回周日日期:2014.01.05
DATA date TYPE sy-datum.

CALL FUNCTION 'WEEK_GET_FIRST_DAY'
EXPORTING
week = '' "输入周
IMPORTING
date = date "返回该周第一天(周一):2013.12.30
EXCEPTIONS
week_invalid =
OTHERS = .

F4_DATE 函数

PARAMETERS:p1() 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 =
date_after_range =
date_before_range =
date_invalid =
factory_calendar_not_found =
holiday_calendar_not_found =
parameter_conflict =
OTHERS = .

SAP ABAP 日期,时间 相关函数

F4_CLOCK 函数

PARAMETERS:p1() 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 日期,时间 相关函数

F4 弹出框的形式选择年月

PARAMETERS: p_year   TYPE mard-lfgja DEFAULT sy-datum+(),
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+() 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 = "弹出框屏幕位置
start_row = "弹出框屏幕位置
IMPORTING
selected_month = selected_month "返回用户输入月份
return_code = return_code "返回 sy-subrc
EXCEPTIONS
factory_calendar_not_found =
holiday_calendar_not_found =
month_not_found =
OTHERS = . IF return_code = . p_year = selected_month+().
p_month = selected_month+(). ENDIF.

SAP ABAP 日期,时间 相关函数

弹出选择周的对话框

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 日期,时间 相关函数

12小时制与24小时制的时间转换  

DATA: lv_in_time  TYPE tims,
lv_out_time TYPE tims,
lv_am_pm TYPE c.
lv_in_time = ''.
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 =
OTHERS = . 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'
EXPORTING
DATIN =
FORMAT =
* IMPORTING
* DATEX =
.
DATA date2() TYPE c.

CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL'
EXPORTING
date_internal = ''
IMPORTING
date_external = date2 "根据用户日期格式参数输出:YYYY.MM.DD
EXCEPTIONS
date_internal_is_invalid =
OTHERS = .

时间戳

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

把时间和日期转成时间戳

CALL FUNCTION 'CIF_GEN4_CONVERT_DATETIME'
EXPORTING
iv_date = l_datum1
IV_TIME = l_time1
IV_TIMEZONE = SY-ZONLO
IMPORTING
EV_TIMESTAMP = l_timestamp1
EXCEPTIONS
TIME_CONVERSION_FAILED =
OTHERS =
.
CALL FUNCTION 'IB_CONVERT_INTO_TIMESTAMP'
EXPORTING
I_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 = ''.
IF sy-subrc = .
SELECT SINGLE * FROM adrc WHERE addrnumber = t001w-adrnr . "time_zone
PERFORM date_to_timestamp USING '' '' adrc-time_zone
CHANGING tstamp.
WRITE tstamp.
ENDIF.
IF NOT tstamp IS INITIAL.
PERFORM timestamp_to_date USING tstamp adrc-time_zone
CHANGING date time.
WRITE / : date,time .
ENDIF. *&---------------------------------------------------------------------*
*& Form date_to_timestamp
*&---------------------------------------------------------------------*
* 日期+时间+时区 转变为 时间戳
*----------------------------------------------------------------------*
FORM date_to_timestamp USING i_date LIKE sy-datum
i_time LIKE sy-uzeit
i_tzone LIKE tzonref-tzone
CHANGING 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 .
MESSAGE e000(oo) WITH '时区错'.
WHEN .
MESSAGE e000(oo) WITH '时间戳错误'.
WHEN OTHERS.
ENDCASE.
ENDFORM. "date_to_timestamp *&---------------------------------------------------------------------*
*& Form TIMESTAMP_TO_DATE
*&---------------------------------------------------------------------*
* 时间戳+时区 转变为 日期和时间
*----------------------------------------------------------------------*
FORM timestamp_to_date USING i_tstamp LIKE tzonref-tstamps
i_tzone LIKE tzonref-tzone
CHANGING o_date LIKE sy-datum
o_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 .
MESSAGE e000(oo) WITH '时区错'.
WHEN .
MESSAGE e000(oo) WITH '时间戳错误'.
WHEN OTHERS.
ENDCASE.
ENDFORM. "TIMESTAMP_TO_DATE

把时间戳转成时间和日期

CALL FUNCTION 'CIF_GEN4_CONVERT_TIMESTAMP'
EXPORTING
iv_timestamp = l_timestamp3
IV_TIMEZONE = SY-ZONLO
IMPORTING
EV_DATE = l_datum3
EV_TIME = l_time3
EXCEPTIONS
TIME_CONVERSION_FAILED =
OTHERS =
.
上一篇:Redis各种数据结构性能数据对比和性能优化实践


下一篇:mongodb拷贝数据库copyDatabase()。实现释放磁盘空间的方法。