ABAP学习(9):时间⽇期操作
ABAP的时间⽇期操作
ABAP的⽇期类型D,是⼀个8位字符串;时间类型T,是6位字符串。⽇期实际是⼀个字符串,格式;‘YYYYMMDD’,可以通过字符串截取获取年⽉⽇字符⼦串。
⽰例:使⽤字符串截取出年份、⽉份等
"⽇期
DATA:temp_date(8) TYPE C.
temp_date = sy-datum.
WRITE:/ temp_date.
temp_date = temp_date+2(2).
WRITE:/ temp_date.
1、判断⽇期是否在指定范围
⽰例:
WRITE:/'测试⽇期是否在⼀个时间段中'.
DATA:date1 type d,
date2 type d.
date1 = '20181201'.
date2 = '20191221'.
if sy-datum > date1 and sy-datum < date2.
WRITE:/ sy-datum,'在范围内'.
else.
WRITE:/ sy-datum,'不在范围内'.
ENDIF.
2、判断⽇期是星期⼏
使⽤function,’DAY_IN_WEEK’判断⼀个⽇期是星期⼏
⽰例:
WRITE:'根据⽇期获取⽇期星期⼏'.
DATA: wotnr type p.
CALL FUNCTION'DAY_IN_WEEK'
EXPORTING
DATUM = sy-datum
IMPORTING
WOTNR = wotnr.
WRITE:/ sy-datum,'是星期',wotnr.
3、根据⽇期获取⽇期所在周数
使⽤function,’DATE_GET_WEEK’,获取⽇期所在周数
⽰例:
CALL FUNCTION'DATE_GET_WEEK'
EXPORTING
DATE = sy-datum
IMPORTING
WEEK = week
4、根据周数获取本周第⼀天⽇期
⽰例:
WRITE:/ '测试WEEK_GET_FIRST_DAY ,通过周数获取,本周第⼀天'. DATA: date3 type d.
CALL FUNCTION'WEEK_GET_FIRST_DAY'
EXPORTING
WEEK = week
IMPORTING
DATE = date3
5、检查⽇期格式是否正确
⽰例:
WRITE:/ '测试DATE_CHECK_FAILED ,检查⽇期是否正确'.
data: date4 type d.
data: date5 type d.
data: date6 type d.
date4 = '20101231'. "需要这种格式⽇期
date5 = '12/23/2019'. "不能识别
date6 = '12312010'. "不能识别
CALL FUNCTION'DATE_CHECK_PLAUSIBILITY'
EXPORTING
DATE = date6
EXCEPTIONS
PLAUSIBILITY_CHECK_FAILED = 1
OTHERS = 2.
6、根据输⼊⽇期获取所在⽉最后⼀天
⽰例:
WRITE:/ '测试RP_LAST_DAY_OF_MONTHS,获取输⼊⽇期所在⽉最后⼀天⽇期'. data:date7 type d.
CALL FUNCTION'RP_LAST_DAY_OF_MONTHS'
EXPORTING
DAY_IN = sy-datum
IMPORTING
LAST_DAY_OF_MONTH = date7
EXCEPTIONS
DAY_IN_NO_DATE = 1.
* OTHERS = 2
7、将输⼊⽇期进⾏计算
⽰例:
WRITE:/'测试RP_CALC_DATE_IN_INTERVAL ,⽇期加减年,⽉,⽇计算'. data: date8 type d value '20181201'.
data: date9 type d.
CALL FUNCTION'RP_CALC_DATE_IN_INTERVAL'
EXPORTING
DATE = date8
DAYS = 20"天
MONTHS = 0"⽉
SIGNUM = '-'"运算符号
YEARS = 1"年
IMPORTING
CALC_DATE = date9.
WRITE:/ date9.
8、获取时间和⽇期之差
⽰例:
WRITE:/ '测试SD_DATETIME_DIFFERENCE,获取⽇期,和时间之间差'. DATA:date10 type d value '20180911'.
data:date11 type d value '20180922'.
DATA:date12 type p.
data:timediff type p.
CALL FUNCTION'SD_DATETIME_DIFFERENCE'
EXPORTING
DATE1 = date10
TIME1 = '080000'"必须为 hhmmss格式
DATE2 = date11
TIME2 = '120000'
IMPORTING
DATEDIFF = date12
TIMEDIFF = timediff.
9、获取每个⽉名字
⽰例:
WRITE:/ '获取每个⽉的名字'.
DATA: month_name like t247 OCCURS0WITH HEADER LINE.
CALL FUNCTION'MONTH_NAMES_GET'
* EXPORTING
* LANGUAGE = SY-LANGU
* IMPORTING
* RETURN_CODE =
TABLES
MONTH_NAMES = month_name.
10、获取⼀段时间内节⽇⽇期
⽰例:
WRITE:/'获取节⽇表'.
DATA: holidays like iscal_day OCCURS0WITH HEADER LINE.
DATA: valid_year_from like SCAL-YEAR.
valid from是什么意思DATA: valid_year_to like SCAL-YEAR.
DATA: return_code like sy-subrc.
CALL FUNCTION'HOLIDAY_GET'
EXPORTING
HOLIDAY_CALENDAR = 'CN'"获取holiday calendar id, tcode:scal
FACTORY_CALENDAR = 'CN'"获取⼯⼚ calendar id ,通过tcode :SCAL
DATE_FROM = '20180102'
DATE_TO = '20181111'
IMPORTING
YEAR_OF_VALID_FROM = valid_year_from
YEAR_OF_VALID_TO = valid_year_to
RETURNCODE = return_code
TABLES
HOLIDAYS = holidays.
11、判断⽇期是否是假⽇
使⽤function,’HOLIDAY_CHECK_AND_GET_INFO’判断是否是假⽇,如果是返回值HOLIDAY_FOUND等于‘X’。⽰例:
WRITE:'判断是否是假⽇'.
DATA:holiday_indicator type scal-indicator.
CALL FUNCTION'HOLIDAY_CHECK_AND_GET_INFO'
EXPORTING
DATE = '20181001'
HOLIDAY_CALENDAR_ID = 'CN'
* WITH_HOLIDAY_ATTRIBUTES = ' '
IMPORTING
HOLIDAY_FOUND = holiday_indicator.
12、获取当前年⽉
⽰例:
WRITE:/'获取当前年份⽉份'.
DATA:cur_year like BKPF-GJAHR.
DATA:cur_month like BKPF-MONAT.
DATA:pre_year like BKPF-GJAHR.
DATA:pre_month like BKPF-MONAT.
CALL FUNCTION'GET_CURRENT_YEAR' EXPORTING
BUKRS = 'BV02'
DATE = SY-DATUM
IMPORTING
CURRM = cur_month
CURRY = cur_year
PREVM = pre_month
PREVY = pre_year
.
WRITE:/'当前年⽉',cur_year,'--',cur_month.
WRITE:/'前⼀年⽉',pre_year,'--',pre_month.
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论