轉自:http://www.sapjx.com/abap-datetime-function.html
| 函數名稱 (內頁-點擊名稱可查看操作) | 函數說明 | 備注 |
| 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.
"上期會計年度
|
