当前位置:   article > 正文

ABAP日期时间函数(转载)_abap 日期加天数

abap 日期加天数

FIMA_DATE_CREATE 函数

获取输入日期前、后的年、月、日

  1. DATA: date TYPE vtbbewe-dvalut,
  2. flag TYPE trff_flg,
  3. days TYPE trff_type_n_2.
  4. CALL FUNCTION 'FIMA_DATE_CREATE'
  5. EXPORTING
  6. i_date = '20140101' "输入日期
  7. i_flg_end_of_month = ' '
  8. i_years = 2 "两年后的日期. 即输入日期的年加2所得日期,可为负数,表示前两年
  9. i_months = 1 "一个月后的日期.即输入日期的月加1所得日期,可为负数,表示前一个月
  10. i_days = 23 "23天后的日期。可为负数,表示23天前的日期
  11. i_calendar_days = 10 "10天后的日历。同I_DAYS参数。
  12. i_set_last_day_of_month = 'X' "返回的日期为当前月份的最后一天
  13. IMPORTING
  14. e_date = date "返回的日期为当前月份的最后一天
  15. e_flg_end_of_month = flag "如果输入参数I_SET_LAST_DAY_OF_MONTH设置’X’了,则返回值为’X’。
  16. e_days_of_i_date = days. "返回输入日期的i_calendar_days字段与I_DAYS字段的和。
  17. 输出结果:
  18. date:2016.03.31
  19. flag:X
  20. days:24

RP_CALC_DATE_IN_INTERVAL 函数

获取输入日期前、后的年、月、日

  1. DATA calc_date TYPE p0001-begda.
  2. CALL FUNCTION 'RP_CALC_DATE_IN_INTERVAL'
  3. EXPORTING
  4. date = '20140101' "输入日期
  5. days = 10 "天数
  6. months = 0 "月数
  7. signum = '+' "+号:表示 N天//年后的日期, -号:表示过去的日期
  8. years = 0 "年数
  9. IMPORTING
  10. calc_date = calc_date. "返回结果:10天后的日期(2014.01.11

LAST_DAY_OF_MONTHS 函数

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

  1. DATA date TYPE sy-datum.
  2. CALL FUNCTION 'LAST_DAY_OF_MONTHS'
  3. EXPORTING
  4. day_in = '20140101' "输入日期
  5. IMPORTING
  6. last_day_of_month = date "返回日期:20140131
  7. EXCEPTIONS
  8. day_in_no_date = 1
  9. OTHERS = 2.

RP_LAST_DAY_OF_MONTHS 函数

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

  1. DATA date TYPE sy-datum.
  2. CALL FUNCTION 'RP_LAST_DAY_OF_MONTHS'
  3. EXPORTING
  4. day_in = '20140101' "输入日期
  5. IMPORTING
  6. last_day_of_month = date "返回日期:20140131
  7. EXCEPTIONS
  8. day_in_no_date = 1
  9. OTHERS = 2.

BKK_GET_MONTH_LASTDAY 函数

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

  1. DATA date TYPE sy-datum.
  2. CALL FUNCTION 'BKK_GET_MONTH_LASTDAY'
  3. EXPORTING
  4. i_date = '20140101' "输入日期
  5. IMPORTING
  6. e_date = date. "返回日期:20140131

CCM_GO_BACK_MONTHS 函数

获取输入日期过去N月的日期

  1. DATA date TYPE sy-datum.
  2. CALL FUNCTION 'CCM_GO_BACK_MONTHS'
  3. EXPORTING
  4. currdate = '20140101' "输入日期
  5. backmonths = 3 "过去月数
  6. IMPORTING
  7. newdate = date. "返回日期:20131001

MONTH_PLUS_DETERMINE 函数

获取输入日期后来N月的日期

  1. DATA date TYPE sy-datum.
  2. CALL FUNCTION 'MONTH_PLUS_DETERMINE'
  3. EXPORTING
  4. months = 3
  5. olddate = '20140101' "输入日期
  6. IMPORTING
  7. newdate = date. "返回日期:20140401

DATE_GET_WEEK 函数

获取输入日期该年的周数,输出格式为:YYYYWW。

  1. DATA week TYPE scal-week.
  2. CALL FUNCTION 'DATE_GET_WEEK'
  3. EXPORTING
  4. date = '20140101' "输入日期
  5. IMPORTING
  6. week = week "返回周数:201401
  7. EXCEPTIONS
  8. date_invalid = 1
  9. OTHERS = 2.

WEEK_GET_FIRST_DAY 函数

获取输入周数的第一天

  1. DATA date TYPE sy-datum.
  2. CALL FUNCTION 'WEEK_GET_FIRST_DAY'
  3. EXPORTING
  4. week = '201401' "输入周
  5. IMPORTING
  6. date = date "返回该周第一天(周一):2013.12.30
  7. EXCEPTIONS
  8. week_invalid = 1
  9. OTHERS = 2.

DAY_IN_WEEK 函数

获取输入日期该天是星期几/周几

  1. DATA wotnr TYPE p.
  2. CALL FUNCTION 'DAY_IN_WEEK'
  3. EXPORTING
  4. datum = '20140101' "输入日期
  5. IMPORTING
  6. wotnr = wotnr. "返回:3 =》星期三/周三

GET_WEEK_INFO_BASED_ON_DATE 函数

获取输入日期该年的周数和这周周一、周日日期(周起始日是周一)

  1. DATA: week TYPE scal-week ,
  2. monday TYPE sy-datum,
  3. sunday TYPE sy-datum.
  4. CALL FUNCTION 'GET_WEEK_INFO_BASED_ON_DATE'
  5. EXPORTING
  6. date = '20140101'
  7. IMPORTING
  8. week = week "返回周数:201401
  9. monday = monday "返回周一日期:2013.12.30
  10. sunday = sunday. "返回周日日期:2014.01.05

HR_GBSSP_GET_WEEK_DATES 函数

获取查询日期该年的第几周和这周周一、周日日期,该天是星期几/周几(周起始日是周日)

  1. DATA:sunday TYPE sy-datum,
  2. saturday TYPE sy-datum,
  3. day_in_week TYPE i,
  4. week_no TYPE p08_weekno.
  5. CALL FUNCTION 'HR_GBSSP_GET_WEEK_DATES'
  6. EXPORTING
  7. p_pdate = '20140101' "输入日期
  8. IMPORTING
  9. p_sunday = sunday "返回本周开始日期(周日):2013.12.29
  10. p_saturday = saturday "返回本周结束日期(周六):2014.01.04
  11. p_day_in_week = day_in_week "返回该日星期几/周几: 4 => 星期三/周三 (这周中的第4天,由于开始日期是周日,故需要-1)
  12. p_week_no = week_no. "返回本周开始日期的周数:201352

HOLIDAY_CHECK_AND_GET_INFO 函数

判断某天是否是假日

  1. DATA holiday TYPE c.
  2. DATA attributes TYPE SORTED TABLE OF thol.
  3. CALL FUNCTION 'HOLIDAY_CHECK_AND_GET_INFO'
  4. EXPORTING
  5. date = '20140201' "输入日期:春节
  6. holiday_calendar_id = 'ZS' "假日日历 ID
  7. with_holiday_attributes = ' ' "属性标识
  8. IMPORTING
  9. holiday_found = holiday "输出是否为假日的标记 输出结果:X
  10. TABLES
  11. holiday_attributes = attributes "输出该假日的属性
  12. "WITH_HOLIDAY_ATTRIBUTES参数 = X,则会带出属性记录
  13. EXCEPTIONS
  14. calendar_buffer_not_loadable = 1
  15. date_after_range = 2
  16. date_before_range = 3
  17. date_invalid = 4
  18. holiday_calendar_id_missing = 5
  19. holiday_calendar_not_found = 6
  20. OTHERS = 7.

CONVERT_DATE_TO_INTERNAL 函数

将标准日期格式转换为内部数字格式

日期的格式与用户参数有关,转化为内部数字格式时,都为:YYYYMMDD.

  1. DATA date TYPE sy-datum.
  2. CALL FUNCTION 'CONVERT_DATE_TO_INTERNAL'
  3. EXPORTING
  4. date_external = '2014.01.01' "当前用户日期格式:YYYY.MM.DD
  5. accept_initial_date = ' '
  6. IMPORTING
  7. date_internal = date "输出20140101
  8. EXCEPTIONS
  9. date_external_is_invalid = 1
  10. OTHERS = 2.

CONVERT_DATE_TO_EXTERNAL 函数

将内部数字日期格式转换为系统日期格式

系统日期格式输出时,与用户参数设置有关

  1. DATA date2(10) TYPE c.
  2. CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL'
  3. EXPORTING
  4. date_internal = '20140101'
  5. IMPORTING
  6. date_external = date2 "根据用户日期格式参数输出:YYYY.MM.DD
  7. EXCEPTIONS
  8. date_internal_is_invalid = 1
  9. OTHERS = 2.

DATE_CONVERT_TO_FACTORYDATE 函数

根据日期返回工厂日历日期

  1. DATA: date TYPE scal-date,
  2. factorydate TYPE facdate,
  3. workingday TYPE scal-indicator.
  4. CALL FUNCTION 'DATE_CONVERT_TO_FACTORYDATE'
  5. EXPORTING
  6. correct_option = '+' "表示工作日计算方式的标记
  7. date = '20140101' "必须转换的日期
  8. factory_calendar_id = 'ZS' "工厂日历ID
  9. IMPORTING
  10. date = date "工厂日历日期
  11. factorydate = factorydate "指定日历中的工作日数
  12. workingday_indicator = workingday "表示某日期是否为工作日的标志
  13. EXCEPTIONS
  14. calendar_buffer_not_loadable = 1
  15. correct_option_invalid = 2
  16. date_after_range = 3
  17. date_before_range = 4
  18. date_invalid = 5
  19. factory_calendar_not_found = 6
  20. OTHERS = 7.
  21. 输出结果:
  22. date: 20140102
  23. factorydate: 1531
  24. workingday: +

HR_99S_INTERVAL_BETWEEN_DATES 函数

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

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

  1. TYPE-POOLS: p99sg.
  2. DATA:days TYPE i,
  3. weeks TYPE i,
  4. months TYPE i,
  5. years TYPE i.
  6. DATA: month_tab TYPE STANDARD TABLE OF p99sg_month_tab_row.
  7. CALL FUNCTION 'HR_99S_INTERVAL_BETWEEN_DATES'
  8. EXPORTING
  9. begda = '20130101' "开始日期
  10. endda = '20140101' "截止日期
  11. IMPORTING
  12. days = days "返回天数:266
  13. c_weeks = weeks "返回周数:52
  14. c_months = months "返回月数:12
  15. c_years = years "返回年数:1
  16. month_tab = month_tab. "期间月份首尾日

month_tab内容明细:

FIMA_DAYS_AND_MONTHS_AND_YEARS 函数

计算两个日期期间的天数

  1. *不单独计算
  2. DATA:days TYPE i,
  3. months TYPE i,
  4. years TYPE i.
  5. CALL FUNCTION 'FIMA_DAYS_AND_MONTHS_AND_YEARS'
  6. EXPORTING
  7. i_date_from = '20130101'
  8. i_date_to = '20141001'
  9. i_flg_separate = ' ' "是否单独计算 若有标识,则返回两日期相差的天数
  10. IMPORTING
  11. e_days = days "返回天数:638
  12. e_months = months "返回月数:21
  13. e_years = years. "返回年数:2
  14. * 单独计算
  15. CALL FUNCTION 'FIMA_DAYS_AND_MONTHS_AND_YEARS'
  16. EXPORTING
  17. i_date_from = '20130101'
  18. i_date_to = '20141001'
  19. i_flg_separate = 'X' "标识 'X'
  20. IMPORTING
  21. e_days = days "返回天数:0
  22. e_months = months "返回月数:9
  23. e_years = years. "返回年数:1

FIMA_DATE_COMPARE 函数

比较两个日期大小,返回结果是关系操作符:GT、EQ、LT

  1. TYPE-POOLS trff .
  2. DATA log_op TYPE trff_type_c_2.
  3. CALL FUNCTION 'FIMA_DATE_COMPARE'
  4. EXPORTING
  5. i_flg_intraday = ' '
  6. i_date = '20140101'
  7. i_time = '000000'
  8. i_sincl = '0' "计算期间开始的排除标识
  9. i_sult = ' ' "计算期间开始的月末标识符
  10. i_comp_date = '20140201' "比较日期
  11. i_comp_time = '000000' "比较时间
  12. i_comp_sincl = '0' "计算期间结束的包含标识
  13. i_comp_sult = ' ' "计算期间结束的月末标识符
  14. IMPORTING
  15. e_log_op = log_op. "返回比较结果:LT

SD_DATETIME_DIFFERENCE 函数

两个日期作差,即是两个日期相减,包括当天时间。

  1. DATA: datediff TYPE p,
  2. timediff TYPE p,
  3. earliest TYPE c.
  4. CALL FUNCTION 'SD_DATETIME_DIFFERENCE'
  5. EXPORTING
  6. date1 = '20140101'
  7. time1 = '240000'
  8. date2 = '20140101'
  9. time2 = '083000'
  10. IMPORTING
  11. datediff = datediff "返回日期差:0
  12. timediff = timediff "返回时间差:16
  13. earliest = earliest "返回时间正负:2,1-负 0-相等 2-正
  14. EXCEPTIONS
  15. invalid_datetime = 1
  16. OTHERS = 2.

HRVE_CONVERT_TIME 函数

12小时制与24小时制的时间转换,例如:07:00:00 pm -> 19:00:00。

  1. DATA:lv_in_time TYPE tims,
  2. lv_out_time TYPE tims,
  3. lv_am_pm TYPE c.
  4. lv_in_time = '060000'.
  5. CALL FUNCTION 'HRVE_CONVERT_TIME'
  6. EXPORTING
  7. type_time = 'B' " A = 24小时制 -> 12小时制 B = 12小时制 -> 24小时制
  8. input_time = lv_in_time
  9. input_am_pm = 'PM'
  10. IMPORTING
  11. output_time = lv_out_time
  12. output_am_pm = lv_am_pm
  13. EXCEPTIONS
  14. parameter_error = 1
  15. OTHERS = 2.
  16. WRITE:/ | Input Time - { lv_in_time }|. " 输出:060000
  17. WRITE:/ |Output Time - { lv_out_time }|. " 输出:180000

MONTH_NAMES_GET 函数

获取所有的月份名字信息

  1. DATA return_code TYPE sy-subrc.
  2. DATA month_names TYPE STANDARD TABLE OF t247.
  3. CALL FUNCTION 'MONTH_NAMES_GET'
  4. EXPORTING
  5. language = sy-langu "系统当前语言
  6. IMPORTING
  7. return_code = return_code "返回 SY-SUBRC
  8. TABLES
  9. month_names = month_names "返回月份信息
  10. EXCEPTIONS
  11. month_names_not_found = 1
  12. OTHERS = 2.

month_names 明细内容:

WEEKDAY_GET 函数

获取所有的星期名字

  1. DATA return_code TYPE sy-subrc.
  2. DATA weekday TYPE STANDARD TABLE OF t246.
  3. CALL FUNCTION 'WEEKDAY_GET'
  4. EXPORTING
  5. language = sy-langu "系统当前语言
  6. IMPORTING
  7. return_code = return_code "返回 SY-SUBRC
  8. TABLES
  9. weekday = weekday "返回星期名字信息
  10. EXCEPTIONS
  11. weekday_not_found = 1
  12. OTHERS = 2.

weekday 内容明细:

DAY_ATTRIBUTES_GET 函数

查看日期属性 包括:该日期是星期几,第几天,是否公共假期等;

公共假期等内容需要输入国家日历。

  1. DATA: year_of_valid_from TYPE scal-year,
  2. year_of_valid_to TYPE scal-year,
  3. return_code TYPE sy-subrc.
  4. DATA day_attributes TYPE STANDARD TABLE OF casdayattr.
  5. CALL FUNCTION 'DAY_ATTRIBUTES_GET'
  6. EXPORTING
  7. factory_calendar = ' ' "工厂日历ID
  8. holiday_calendar = ' ' "假期日历ID
  9. date_from = '20140101' "开始日期
  10. date_to = '20140110' "截止日期
  11. language = sy-langu "系统当前语言
  12. non_iso = ' '
  13. IMPORTING
  14. year_of_valid_from = year_of_valid_from
  15. year_of_valid_to = year_of_valid_to
  16. returncode = return_code
  17. TABLES
  18. day_attributes = day_attributes "返回日期每日属性
  19. EXCEPTIONS
  20. factory_calendar_not_found = 1
  21. holiday_calendar_not_found = 2
  22. date_has_invalid_format = 3
  23. date_inconsistency = 4
  24. OTHERS = 5.

day_attributes 内容明细:

F4_DATE 函数

为 F4 帮助显示日历,弹出日历对话框,供用户选择日期

  1. PARAMETERS:p1(6) TYPE c.
  2. AT SELECTION-SCREEN ON VALUE-REQUEST FOR p1.
  3. DATA:l_date TYPE sy-datum.
  4. CALL FUNCTION 'F4_DATE'
  5. EXPORTING
  6. date_for_first_month = sy-datum
  7. IMPORTING
  8. select_date = l_date "用户选择后返回的日期
  9. EXCEPTIONS
  10. calendar_buffer_not_loadable = 1
  11. date_after_range = 2
  12. date_before_range = 3
  13. date_invalid = 4
  14. factory_calendar_not_found = 5
  15. holiday_calendar_not_found = 6
  16. parameter_conflict = 7
  17. OTHERS = 8.

显示效果:

F4_CLOCK 函数

为 F4 帮助显示时间,弹出时间对话框,供用户选择时间

  1. PARAMETERS:p1(6) TYPE c.
  2. AT SELECTION-SCREEN ON VALUE-REQUEST FOR p1.
  3. DATA: l_time TYPE sy-uzeit.
  4. CALL FUNCTION 'F4_CLOCK'
  5. EXPORTING
  6. start_time = sy-uzeit
  7. display = ' '
  8. IMPORTING
  9. selected_time = l_time.

效果显示:

POPUP_TO_SELECT_MONTH 函数

为 F4 帮助显示月份:弹出选择年和月的对话框

  1. PARAMETERS: p_year TYPE mard-lfgja DEFAULT sy-datum+0(4),
  2. p_month TYPE mard-lfmon.
  3. AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_month.
  4. DATA: actual_month LIKE isellist-month,
  5. selected_month LIKE isellist-month,
  6. return_code TYPE sy-subrc.
  7. CONCATENATE p_year sy-datum+4(2) INTO actual_month.
  8. CALL FUNCTION 'POPUP_TO_SELECT_MONTH'
  9. EXPORTING
  10. actual_month = actual_month "传入年份
  11. factory_calendar = ' ' "工厂日历 ID
  12. holiday_calendar = ' ' "假日日历 ID
  13. language = sy-langu "当前系统语言
  14. start_column = 8 "弹出框屏幕位置
  15. start_row = 5 "弹出框屏幕位置
  16. IMPORTING
  17. selected_month = selected_month "返回用户输入月份
  18. return_code = return_code "返回 sy-subrc
  19. EXCEPTIONS
  20. factory_calendar_not_found = 1
  21. holiday_calendar_not_found = 2
  22. month_not_found = 3
  23. OTHERS = 4.

效果显示:

POPUP_CALENDAR_SDB 函数

弹出选择周的对话框

  1. DATA: begin_date TYPE sy-datum,
  2. end_date TYPE sy-datum.
  3. CALL FUNCTION 'POPUP_CALENDAR_SDB'
  4. EXPORTING
  5. sel_day = 'X' "启用天 选择
  6. sel_week = 'X' "启用周 选择
  7. sel_month = 'X' "启用月 选择
  8. sel_interval = 'X'
  9. focus_day = sy-datum "默认选择日期
  10. IMPORTING
  11. begin_date = begin_date "返回选择周第一天
  12. end_date = end_date. "返回选择周最后一天

效果显示:

GET_CURRENT_YEAR 函数

获得当前的财政年(Fiscal year)

  1. DATA: currm TYPE bkpf-monat,
  2. curry TYPE bkpf-gjahr,
  3. prevm TYPE bkpf-monat,
  4. prevy TYPE bkpf-gjahr.
  5. CALL FUNCTION 'GET_CURRENT_YEAR'
  6. EXPORTING
  7. bukrs = '3010' "输入公司代码
  8. date = sy-datum "输入日期
  9. IMPORTING
  10. currm = currm "当前会计期间
  11. curry = curry "当前会计年度
  12. prevm = prevm "上期会计期间
  13. prevy = prevy. "上期会计年度
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/311169
推荐阅读
相关标签
  

闽ICP备14008679号