matinal:ABAP中常用的日期处理函数

2023-10-14 14:02:42 浏览数 (1)

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

代码语言:javascript复制
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.获取两个日期之间的天、周、月、年数:#
代码语言:javascript复制
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.获取输入日期的星期数:#
代码语言:javascript复制
DATA lv_week TYPE p.
CALL FUNCTION 'DAY_IN_WEEK'
  EXPORTING
    datum         =  '20200101'   "输入日期"
 IMPORTING
   wotnr         =    lv_week.     "返回:3"
4.转换日期为内部数字格式:#
代码语言:javascript复制
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.转换日期为外部数字格式:#
代码语言:javascript复制
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 月的日期:#
代码语言:javascript复制
DATA date TYPE sy-datum.
CALL FUNCTION 'CCM_GO_BACK_MONTHS'
  EXPORTING
    currdate   = '20140101'    "输入日期"
    backmonths = 3             "过去3月"
  IMPORTING
    newdate    = date.         "20131001"
7.获取未来 N 月的日期:#
代码语言:javascript复制
DATA date TYPE sy-datum.
CALL FUNCTION 'MONTH_PLUS_DETERMINE'
  EXPORTING
    months  = 4             "未来4月"
    olddate = '20140101'    "输入日期"
  IMPORTING
    newdate = date.         "20140501"
8.F4 日期选择框:#
代码语言:javascript复制
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 时间选择框:#
代码语言:javascript复制
DATA sel_time TYPE sy-uzeit.
CALL FUNCTION 'F4_CLOCK'
  EXPORTING
    start_time    = sy-uzeit    "默认系统当前时间"
  IMPORTING
    selected_time = sel_time.   "用户选择时间"
10.获取两个日期之间的详细信息:#
代码语言:javascript复制
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.日期转时间戳:#
代码语言:javascript复制
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.时间戳转日期和时间:#
代码语言:javascript复制
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.

0 人点赞