转自:http://www.sapjx.com/abap-datetime-function.html
ABAP日期时间函数收集:
函数名称 (内页-点击名称可查看操作) | 函数说明 | 备注 |
FIMA_DATE_CREATE | 获取查询日期前、后的年、月、日 | |
LAST_DAY_OF_MONTHS | 获取查询日期最后一天的日期 | |
CCM_GO_BACK_MONTHS | 获取查询日期过去N月的日期 |
MONTH_PLUS_DETERMINE | 获取查询日期未来N月的日期 | |
DATE_GET_WEEK | 获取查询日期该年的第几周 | 输出格式:YYYYWW |
WEEK_GET_FIRST_DAY | 获取查询周数的第一天 | |
DAY_IN_WEEK | 获取查询日期该天是星期几 | |
GET_WEEK_INFO_BASED_ON_DATE | 获取查询日期该年的第几周和这周周一、周日日期 | 周起始日是周一 |
HR_GBSSP_GET_WEEK_DATES | 获取查询日期该年的第几周和这周周一、周日日期,该天是周几 | 周起始日是周日 |
DATE_CHECK_PLAUSIBILITY | 检查日期有效性 | SY-SUBRC = 0 有效 |
TIME_CHECK_PLAUSIBILITY | 检查时间有效性 | SY-SUBRC = 0 有效 |
HOLIDAY_CHECK_AND_GET_INFO | 判断某天是否是假日 | |
CONVERT_DATE_TO_INTERNAL | 将标准日期格式转换为数字格式 | |
CONVERT_DATE_TO_EXTERNAL | 将数字日期格式转换为系统日期格式 | |
DATE_CONVERT_TO_FACTORYDATE | 将查询日期转为工厂日历日期 | |
HR_99S_INTERVAL_BETWEEN_DATES | 获取两日期间的:天数、周数、月数、年数、期间月份开始截止日期 | |
FIMA_DAYS_AND_MONTHS_AND_YEARS | 计算两日期之间间隔的天数 | |
FIMA_DATE_COMPARE | 比较两个日期大小 | 返回:GT、EQ、LT等 |
SD_DATETIME_DIFFERENCE | 两日期的时间差 | |
HRVE_CONVERT_TIME | 时间:12/24小时制转换 | |
MONTH_NAMES_GET | 获取所有的月份名字 | |
WEEKDAY_GET | 获取所有的星期名字 | |
DAY_ATTRIBUTES_GET | 查看日期属性 包括:该日期是星期几,第几天(周2=2),是不是公共假期等 | 需要输入国家日历 |
F4_DATE | 弹出日历对话框,供用户选择日期 | |
F4_CLOCK | 弹出时间对话框,供用户选择时间 | |
POPUP_TO_SELECT_MONTH | 弹出选择年和月的对话框 | |
POPUP_CALENDAR_SDB | 弹出选择周的对话框 | |
GET_CURRENT_YEAR | 获得当前的财政年(fiscal year) | |
FIRST_DAY_IN_PERIOD_GET | 获得期间首日 | |
LAST_DAY_IN_PERIOD_GET | 获得期间末日 |
函数操作说明:
获取输入日期前、后的年、月、日
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
|
DATA : date TYPE vtbbewe-dvalut, flag TYPE trff_flg, days TYPE trff_type_n_2. CALL FUNCTION 'FIMA_DATE_CREATE' EXPORTING i_date = '20140101' "输入日期 i_flg_end_of_month = ' ' i_years = 2 "两年后的日期. 即输入日期的年加2所得日期,可为负数,表示前两年 i_months = 1 "一个月后的日期.即输入日期的月加1所得日期,可为负数,表示前一个月 i_days = 23 "23天后的日期。可为负数,表示23天前的日期 i_calendar_days = 10 "10天后的日历。同I_DAYS参数。 i_set_last_day_of_month = 'X' "返回的日期为当前月份的最后一天 IMPORTING e_date = date "返回的日期为当前月份的最后一天 e_flg_end_of_month = flag "如果输入参数I_SET_LAST_DAY_OF_MONTH设置’X’了,则返回值为’X’。 e_days_of_i_date = days. "返回输入日期的i_calendar_days字段与I_DAYS字段的和。 |
输出结果:
date:2016.03.31
flag:X
days:24
获取输入日期前、后的年、月、日
01
02
03
04
05
06
07
08
09
10
11
|
DATA calc_date TYPE p0001-begda. CALL FUNCTION 'RP_CALC_DATE_IN_INTERVAL' EXPORTING date = '20140101' "输入日期 days = 10 "天数 months = 0 "月数 signum = '+' "+号:表示 N天/月/年后的日期, -号:表示过去的日期 years = 0 "年数 IMPORTING calc_date = calc_date. "返回结果:10天后的日期(2014.01.11) |
获取输入日期最后一天的日期
01
02
03
04
05
06
07
08
09
10
|
DATA date TYPE sy-datum. CALL FUNCTION 'LAST_DAY_OF_MONTHS' EXPORTING day_in = '20140101' "输入日期 IMPORTING last_day_of_month = date "返回日期:20140131 EXCEPTIONS day_in_no_date = 1 OTHERS = 2. |
获取输入日期最后一天的日期
01
02
03
04
05
06
07
08
09
10
|
DATA date TYPE sy-datum. CALL FUNCTION 'RP_LAST_DAY_OF_MONTHS' EXPORTING day_in = '20140101' "输入日期 IMPORTING last_day_of_month = date "返回日期:20140131 EXCEPTIONS day_in_no_date = 1 OTHERS = 2. |
获取输入日期最后一天的日期
1
2
3
4
5
6
7
|
DATA date TYPE sy-datum. CALL FUNCTION 'BKK_GET_MONTH_LASTDAY' EXPORTING i_date = '20140101' "输入日期 IMPORTING e_date = date. "返回日期:20140131 |
获取输入日期过去N月的日期
1
2
3
4
5
6
7
8
|
DATA date TYPE sy-datum. CALL FUNCTION 'CCM_GO_BACK_MONTHS' EXPORTING currdate = '20140101' "输入日期 backmonths = 3 "过去月数 IMPORTING newdate = date. "返回日期:20131001 |
获取输入日期后来N月的日期
1
2
3
4
5
6
7
8
|
DATA date TYPE sy-datum. CALL FUNCTION 'MONTH_PLUS_DETERMINE' EXPORTING months = 3 olddate = '20140101' "输入日期 IMPORTING newdate = date. "返回日期:20140401 |
获取输入日期该年的周数,输出格式为:YYYYWW。
01
02
03
04
05
06
07
08
09
10
|
DATA week TYPE scal-week. CALL FUNCTION 'DATE_GET_WEEK' EXPORTING date = '20140101' "输入日期 IMPORTING week = week "返回周数:201401 EXCEPTIONS date_invalid = 1 OTHERS = 2. |
获取输入周数的第一天
01
02
03
04
05
06
07
08
09
10
|
DATA date TYPE sy-datum. CALL FUNCTION 'WEEK_GET_FIRST_DAY' EXPORTING week = '201401' "输入周 IMPORTING date = date "返回该周第一天(周一):2013.12.30 EXCEPTIONS week_invalid = 1 OTHERS = 2. |
获取输入日期该天是星期几/周几
1
2
3
4
5
6
7
|
DATA wotnr TYPE p. CALL FUNCTION 'DAY_IN_WEEK' EXPORTING datum = '20140101' "输入日期 IMPORTING wotnr = wotnr. "返回:3 =》星期三/周三 |
GET_WEEK_INFO_BASED_ON_DATE 函数
获取输入日期该年的周数和这周周一、周日日期(周起始日是周一)
01
02
03
04
05
06
07
08
09
10
11
|
DATA : week TYPE scal-week , monday TYPE sy-datum, sunday 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 |
获取查询日期该年的第几周和这周周一、周日日期,该天是星期几/周几(周起始日是周日)
01
02
03
04
05
06
07
08
09
10
11
12
13
14
|
DATA : sunday TYPE sy-datum, saturday TYPE sy-datum, day_in_week TYPE i, week_no TYPE p08_weekno. CALL FUNCTION 'HR_GBSSP_GET_WEEK_DATES' EXPORTING p_pdate = '20140101' "输入日期 IMPORTING p_sunday = sunday "返回本周开始日期(周日):2013.12.29 p_saturday = saturday "返回本周结束日期(周六):2014.01.04 p_day_in_week = day_in_week "返回该日星期几/周几: 4 => 星期三/周三 "(这周中的第4天,由于开始日期是周日,故需要-1) p_week_no = week_no. "返回本周开始日期的周数:201352 |
判断某天是否是假日
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
|
DATA holiday TYPE c. DATA attributes TYPE SORTED TABLE OF thol. CALL FUNCTION 'HOLIDAY_CHECK_AND_GET_INFO' EXPORTING date = '20140201' "输入日期:春节 holiday_calendar_id = 'ZS' "假日日历 ID with_holiday_attributes = ' ' "属性标识 IMPORTING holiday_found = holiday "输出是否为假日的标记 输出结果: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. |
将标准日期格式转换为内部数字格式
日期的格式与用户参数有关,转化为内部数字格式时,都为:YYYYMMDD.
01
02
03
04
05
06
07
08
09
10
11
|
DATA date TYPE sy-datum. CALL FUNCTION 'CONVERT_DATE_TO_INTERNAL' EXPORTING date_external = '2014.01.01' "当前用户日期格式:YYYY.MM.DD accept_initial_date = ' ' IMPORTING date_internal = date "输出20140101 EXCEPTIONS date_external_is_invalid = 1 OTHERS = 2. |
将内部数字日期格式转换为系统日期格式
系统日期格式输出时,与用户参数设置有关
01
02
03
04
05
06
07
08
09
10
|
DATA date2(10) TYPE c. CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL' EXPORTING date_internal = '20140101' IMPORTING date_external = date2 "根据用户日期格式参数输出:YYYY.MM.DD EXCEPTIONS date_internal_is_invalid = 1 OTHERS = 2. |
DATE_CONVERT_TO_FACTORYDATE 函数
根据日期返回工厂日历日期
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
|
DATA : date TYPE scal-date, factorydate TYPE facdate, workingday TYPE scal-indicator. CALL FUNCTION 'DATE_CONVERT_TO_FACTORYDATE' EXPORTING correct_option = '+' "表示工作日计算方式的标记 date = '20140101' "必须转换的日期 factory_calendar_id = 'ZS' "工厂日历ID IMPORTING date = date "工厂日历日期 factorydate = factorydate "指定日历中的工作日数 workingday_indicator = workingday "表示某日期是否为工作日的标志 EXCEPTIONS calendar_buffer_not_loadable = 1 correct_option_invalid = 2 date_after_range = 3 date_before_range = 4 date_invalid = 5 factory_calendar_not_found = 6 OTHERS = 7. |
输出结果:
date: 20140102
factorydate: 1531
workingday: +
HR_99S_INTERVAL_BETWEEN_DATES 函数
获取两个日期期间的:天数、周数、月数、年数;
包括期间月份的开始(月份第一天日期)、截止日期(月份最后一天日期)
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
|
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 = '20130101' "开始日期 endda = '20140101' "截止日期 IMPORTING days = days "返回天数:266 c_weeks = weeks "返回周数:52 c_months = months "返回月数:12 c_years = years "返回年数:1 month_tab = month_tab. "期间月份首尾日 |
month_tab 内容明细:
FIMA_DAYS_AND_MONTHS_AND_YEARS 函数
计算两个日期期间的天数
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
DATA : days TYPE i, months TYPE i, years TYPE i. CALL FUNCTION 'FIMA_DAYS_AND_MONTHS_AND_YEARS' EXPORTING i_date_from = '20130101' i_date_to = '20141001' i_flg_separate = ' ' "是否单独计算 若有标识,则返回两日期相差的天数 IMPORTING e_days = days "返回天数:638 e_months = months "返回月数:21 e_years = years. "返回年数:2 * 单独计算 CALL FUNCTION 'FIMA_DAYS_AND_MONTHS_AND_YEARS' EXPORTING i_date_from = '20130101' i_date_to = '20141001' i_flg_separate = 'X' "标识 'X' IMPORTING e_days = days "返回天数:0 e_months = months "返回月数:9 e_years = years. "返回年数:1 |
比较两个日期大小,返回结果是关系操作符:GT、EQ、LT
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
|
TYPE - POOLS trff . DATA log_op TYPE trff_type_c_2. CALL FUNCTION 'FIMA_DATE_COMPARE' EXPORTING i_flg_intraday = ' ' i_date = '20140101' i_time = '000000' i_sincl = '0' "计算期间开始的排除标识 i_sult = ' ' "计算期间开始的月末标识符 i_comp_date = '20140201' "比较日期 i_comp_time = '000000' "比较时间 i_comp_sincl = '0' "计算期间结束的包含标识 i_comp_sult = ' ' "计算期间结束的月末标识符 IMPORTING e_log_op = log_op. "返回比较结果:LT |
两个日期作差,即是两个日期相减,包括当天时间。
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
|
DATA : datediff TYPE p, timediff TYPE p, earliest TYPE c. CALL FUNCTION 'SD_DATETIME_DIFFERENCE' EXPORTING date1 = '20140101' time1 = '240000' date2 = '20140101' time2 = '083000' IMPORTING datediff = datediff "返回日期差:0 timediff = timediff "返回时间差:16 earliest = earliest "返回时间正负:2,1-负 0-相等 2-正 EXCEPTIONS invalid_datetime = 1 OTHERS = 2. |
12小时制与24小时制的时间转换,例如:07:00:00 pm -> 19:00:00。
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
|
DATA : lv_in_time TYPE tims, lv_out_time TYPE tims, lv_am_pm TYPE c. lv_in_time = '060000' . 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 :/ | Input Time - { lv_in_time }|. " 输出:060000 WRITE :/ |Output Time - { lv_out_time }|. " 输出:180000 |
获取所有的月份名字信息
01
02
03
04
05
06
07
08
09
10
11
12
13
14
|
DATA return_code TYPE sy-subrc. DATA month_names TYPE STANDARD TABLE OF t247. CALL FUNCTION 'MONTH_NAMES_GET' EXPORTING language = sy-langu "系统当前语言 IMPORTING return_code = return_code "返回 SY-SUBRC TABLES month_names = month_names "返回月份信息 EXCEPTIONS month_names_not_found = 1 OTHERS = 2. |
month_names 明细内容:
获取所有的星期名字
01
02
03
04
05
06
07
08
09
10
11
12
13
14
|
DATA return_code TYPE sy-subrc. DATA weekday TYPE STANDARD TABLE OF t246. CALL FUNCTION 'WEEKDAY_GET' EXPORTING language = sy-langu "系统当前语言 IMPORTING return_code = return_code "返回 SY-SUBRC TABLES weekday = weekday "返回星期名字信息 EXCEPTIONS weekday_not_found = 1 OTHERS = 2. |
weekday 内容明细:
查看日期属性 包括:该日期是星期几,第几天,是否公共假期等;
公共假期等内容需要输入国家日历。
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
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. |
day_attributes 内容明细:
为 F4 帮助显示日历,弹出日历对话框,供用户选择日期
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
|
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 帮助显示时间,弹出时间对话框,供用户选择时间
01
02
03
04
05
06
07
08
09
10
11
12
|
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 帮助显示月份:弹出选择年和月的对话框
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
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 . |
效果显示:
弹出选择周的对话框
01
02
03
04
05
06
07
08
09
10
11
12
13
|
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. "返回选择周最后一天 |
效果显示:
获得当前的财政年(Fiscal year)
01
02
03
04
05
06
07
08
09
10
11
12
13
14
|
DATA : currm TYPE bkpf-monat, curry TYPE bkpf-gjahr, prevm TYPE bkpf-monat, prevy TYPE bkpf-gjahr. CALL FUNCTION 'GET_CURRENT_YEAR' EXPORTING bukrs = '3010' "输入公司代码 date = sy-datum "输入日期 IMPORTING currm = currm "当前会计期间 curry = curry "当前会计年度 prevm = prevm "上期会计期间 prevy = prevy. "上期会计年度 |