首页 > 其他分享 >ABAP中常用的日期处理函数

ABAP中常用的日期处理函数

时间:2022-10-19 09:13:26浏览次数:81  
标签:sy DATA ABAP lv 日期 date TYPE 处理函数

 

1.获取输入日期月份的最后一天:#

DATA: lv_date TYPE sy-datum.
CALL FUNCTION 'LAST_DAY_OF_MONTHS'
  EXPORTING
    day_in            = '20140101'     "输入日期
  IMPORTING
    last_day_of_month = lv_date        "返回日期:20140131
  EXCEPTIONS
    day_in_no_date    = 1
    OTHERS            = 2.

2.获取两个日期之间的天、周、月、年数:#

TYPE-POOLS: p99sg.
DATA: lv_days   TYPE i,
      lv_weeks  TYPE i,
      lv_months TYPE i,
      lv_years  TYPE i.
DATA: month_tab TYPE STANDARD TABLE OF p99sg_month_tab_row.
CALL FUNCTION 'HR_99S_INTERVAL_BETWEEN_DATES'
  EXPORTING
    begda     = '20150101'    "开始日期
    endda     = '20160101'    "截止日期
  IMPORTING
    days      = lv_days          "返回天数:266
    c_weeks   = lv_weeks         "返回周数:52
    c_months  = lv_months        "返回月数:12
    c_years   = lv_years         "返回年数:1
    month_tab = month_tab.       "期间月份首尾日

3.获取输入日期的星期数:#

DATA lv_week TYPE p.
CALL FUNCTION 'DAY_IN_WEEK'
  EXPORTING
    datum         =  '20200101'   "输入日期"
 IMPORTING
   wotnr         =    lv_week.     "返回:3"

4.转换日期为内部数字格式:#

DATA: lv_date TYPE sy-datum.
CALL FUNCTION 'CONVERT_DATE_TO_INTERNAL'
  EXPORTING
    date_external            = '2016.01.01'    "当前用户日期格式:YYYY.MM.DD
    accept_initial_date      = ' '
  IMPORTING
    date_internal            = lv_date         "输出:20160101"
  EXCEPTIONS
    date_external_is_invalid = 1
    OTHERS                   = 2.

5.转换日期为外部数字格式:#

DATA: lv_date TYPE sy-datum.
CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL'
  EXPORTING
    date_internal            = '20160101'  "当前日期格式"
  IMPORTING
    date_external            = lv_date     "输出:2016.01.01 "
  EXCEPTIONS
    date_internal_is_invalid = 1
    OTHERS                   = 2.

6.获取过去 N 月的日期:#

DATA date TYPE sy-datum.
CALL FUNCTION 'CCM_GO_BACK_MONTHS'
  EXPORTING
    currdate   = '20140101'    "输入日期"
    backmonths = 3             "过去3月"
  IMPORTING
    newdate    = date.         "20131001"

7.获取未来 N 月的日期:#

DATA date TYPE sy-datum.
CALL FUNCTION 'MONTH_PLUS_DETERMINE'
  EXPORTING
    months  = 4             "未来4月"
    olddate = '20140101'    "输入日期"
  IMPORTING
    newdate = date.         "20140501"

8.F4 日期选择框:#

DATA sel_date TYPE sy-datum.
CALL FUNCTION 'F4_DATE'
 EXPORTING
   DATE_FOR_FIRST_MONTH          = SY-DATUM   "默认系统当前日期"
  IMPORTING
    select_date                  = sel_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.

9.F4 时间选择框:#

DATA sel_time TYPE sy-uzeit.
CALL FUNCTION 'F4_CLOCK'
  EXPORTING
    start_time    = sy-uzeit    "默认系统当前时间"
  IMPORTING
    selected_time = sel_time.   "用户选择时间"

10.获取两个日期之间的详细信息:#

DATA: lv_month_start TYPE dats  ,                     "月初日期"
      lv_month_end   TYPE dats  ,                     "月末日期"
      lv_days        TYPE i     .                     "月总天数"

DATA day_attributes TYPE STANDARD TABLE OF casdayattr WITH HEADER LINE.
CALL FUNCTION 'DAY_ATTRIBUTES_GET'
  EXPORTING
    factory_calendar           = ' '                    "工厂日历ID
    holiday_calendar           = ' '                    "假期日历ID
    date_from                  = lv_month_start         "月开始日期
    date_to                    = lv_month_end           "月结束日期
    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.

*   获取当月总天数
lv_days = lines( day_attributes[] ).

11.日期转时间戳:#

DATA: lv_timestamp   LIKE  tzonref-tstamps.

   CALL FUNCTION 'CIF_GEN4_CONVERT_DATETIME'
     EXPORTING
       iv_date                = sy-datum
       iv_time                = sy-uzeit
*      IV_TIMEZONE            = SY-ZONLO
     IMPORTING
       ev_timestamp           = lv_timestamp
     EXCEPTIONS
       time_conversion_failed = 1
       OTHERS                 = 2.
	   
*或者使用下面语句:
CONVERT DATE sy-datum TIME sy-uzeit INTO TIME STAMP lv_timestamp TIME ZONE sy-zonlo.

12.时间戳转日期和时间:#

DATA: lv_timestamp   LIKE  tzonref-tstamps.

   CALL FUNCTION 'CIF_GEN4_CONVERT_DATETIME'
     EXPORTING
       iv_date                = sy-datum
       iv_time                = sy-uzeit
*      IV_TIMEZONE            = SY-ZONLO
     IMPORTING
       ev_timestamp           = lv_timestamp
     EXCEPTIONS
       time_conversion_failed = 1
       OTHERS                 = 2.
	   
*或者使用下面语句:
CONVERT DATE sy-datum TIME sy-uzeit INTO TIME STAMP lv_timestamp TIME ZONE sy-zonlo.

作者:ITiger

出处:https://www.cnblogs.com/zyhcs/p/15533058.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

转载请注明出处!

标签:sy,DATA,ABAP,lv,日期,date,TYPE,处理函数
From: https://www.cnblogs.com/zt11402/p/16804964.html

相关文章

  • ABAP中常用的功能函数
     1.负号前置:#DATAlv_data(6)TYPEcVALUE'12345-'.CALLFUNCTION'CLOI_PUT_SIGN_IN_FRONT'CHANGINGcvalue=lv_data."-12345"2.读取长文......
  • 工作笔记之 SELECT 语句在 SAP ABAP 中的用法总结(上)
    日常开发过程中,最常用的语句可能就是查询语句了。那么如果从数据库表中查询数据呢?答:​​selectfrom​​语句。SELECT的三个部分在OPENSQL中,​​SELECT​​语句可以查......
  • 连续最长日期
    fromdatetimeimportdatetimedefcalculate_consecutive_dates(dates_str:str):max_consecutive_days=0temp_flag=0dates_list=dates_str.split(",")......
  • clickhouse时间日期函数
    一、常用的时间戳到日期,日期到时间戳操作SELECTtoDateTime(1665936000,'Asia/Shanghai')`时间戳转为:%Y-%m-%d%H:%M:%S`,toUnixTimestamp('2022-10-17')AS`......
  • ABAP 范围表 range table
    范围表定义:DATAgr_testTYPERANGEOFchar6.做选择屏幕的时候范围选择框默认就是一个范围表范围表内容:通过断点调试可以看到,范围表有4列signoptionlowhigh......
  • JavaScript的日期对象的使用Date
    1<script>2letdate=newDate();//返回的是当前的时间3console.log(date.getFullYear());//获取的当前年份4console.log(date.getMonth()+1);......
  • [答疑]商品有出厂日期,人有出生日期,这两个是ppt还是MI
    Derek2018-12-611:19老师,按照您课上说的,带有时间属性的类都可以看作是时刻时段类,假设商品有出厂日期,人有出生日期,这两个是ppt还是MI?潘加宇:带有时间属性的都是事件类,严格来......
  • vue + Reat 日期格式化
    可以在VUE和React上使用,将日期格式化xxxx年xx月xx日在VUE中需要放在filters中,使用方法:时间|formatTimeReact使用方式类似。filters:{formatTime:function(v......
  • ABAP SMARTFORMS和函数_1
                              自定义上传图片       ......
  • Java8中处理日期和时间的常用API
    场景java8中引入了一个新包java.time,包含了多数会用到的核心类。注:博客:https://blog.csdn.net/badao_liumang_qizhi关注公众号霸道的程序猿获取编程相关电子书、教......