• ABAP 中的日期处理 沧海


    Add n number of working days to date

     

     
    
    The following ABAP code adds n number of WORKING days to a particular date, any days which are not workings days (i.e. Saturday) will be ignored. For example if your starting date is Friday, adding 3 days would return a result of Wednesday or if starting date was Wednesday resultant day would be Monday as Saturday and Sunday are non working days. If you want to add n number of working days but allow result to be a non working day then click here for alternative ABAP code.
    
    
     
    
    
    Simply add the below FORM into you code and call it using the usual PERFORM command:
    
    
                     PERFORM add_working_days USING ld_numdays
    
    
                                      CHANGING gd_date.
    
    
     
    
    
              
    
    
    

     

     

     
    
    **&---------------------------------------------------------------------*
    
    
    *&      Form  ADD_WORKING_DAYS
    
    
    *&---------------------------------------------------------------------*
    
    
    *       Add n number of factory days(working days) to date
    
    
    *----------------------------------------------------------------------*
    
    
    *      <-- P_DAYS     Number of days to add
    
    
    *      <-- P_PAYDATE  Starting date
    
    
    *----------------------------------------------------------------------*
    
    
    FORM add_working_days USING p_days
    
    
                          CHANGING p_paydate TYPE sy-datum.
    
    
      DATA: gd_factorydat LIKE scal-facdate,
    
    
            gd_resdate    LIKE sy-datum.
    
    
     
    
    
    * Convert date to factory date
    
    
      CALL FUNCTION 'DATE_CONVERT_TO_FACTORYDATE'
    
    
           EXPORTING
    
    
                date                = p_paydate  "Starting date
    
    
                factory_calendar_id = 'GB'
    
    
           IMPORTING
    
    
                factorydate         = gd_factorydat. "Factory calender date
    
    
     
    
    
    * Add n number of days to factory date, ignors non working days
    
    
      gd_factorydat =  gd_factorydat + p_days.
    
    
     
    
    
    * Convert factory date back to actual date
    
    
      CALL FUNCTION 'FACTORYDATE_CONVERT_TO_DATE'
    
    
           EXPORTING
    
    
                factorydate         = gd_factorydat
    
    
                factory_calendar_id = 'GB'
    
    
           IMPORTING
    
    
                date                = gd_resdate. "Actual date
    
    
     
    
    
      p_paydate = gd_resdate.
    
    
    ENDFORM.                    " ADD_WORKING_DAYS
    
    
    

     


     

    Add n number of working days to date (allow result to be a non working day)

     

     
    
    The following ABAP code adds n number of WORKING days to a particular date, but allows result to be a non working day. For example if starting day is Wednesday and you add three days to it, resultant day would be Saturday(non working day). Where as if you wanted to totally ignore non working days resultant day would be Monday (see ABAP code to totally ignore non working days). 
    
    
     
    
    
    Simply add the below ABAP FORM into you code and call it using the usual PERFORM command:
    
    
                     PERFORM add_working_days_resnonwork USING ld_numdays
    
    
                                                        CHANGING gd_date.
    
    
     
    
    
                        
    
    
    

     

     

     
    
    **&---------------------------------------------------------------------*
    
    
    *&      Form  ADD_WORKING_DAYS_RESNONWORK
    
    
    *&---------------------------------------------------------------------*
    
    
    *       Add n number of factory days(working days) to date, but allow 
    
    
    *       resultant date to be a non working day
    
    
    *----------------------------------------------------------------------*
    
    
    *      <-- P_DAYS     Number of days to add
    
    
    *      <-- P_PAYDATE  Starting date
    
    
    *----------------------------------------------------------------------*
    
    
     
    
    
    FORM add_working_days_resnonwork USING p_days
    
    
                           CHANGING p_paydate TYPE sy-datum.
    
    
     
    
    
      DATA: ld_count  TYPE i.
    
    
     
    
    
      ld_count = p_days.
    
    
     
    
    
      WHILE ld_count GT 0.
    
    
        CALL FUNCTION 'DATE_CHECK_WORKINGDAY'
    
    
          EXPORTING
    
    
            date                       = p_paydate
    
    
            factory_calendar_id        = 'GB'
    
    
            message_type               = 'I'
    
    
          EXCEPTIONS
    
    
            date_after_range           = 1
    
    
            date_before_range          = 2
    
    
            date_invalid               = 3
    
    
            date_no_workingday         = 4
    
    
            factory_calendar_not_found = 5
    
    
            message_type_invalid       = 6
    
    
            OTHERS                     = 7.
    
    
        IF sy-subrc NE  4.
    
    
          p_paydate = p_paydate + 1.
    
    
          ld_count = ld_count - 1.
    
    
        ELSE.
    
    
          p_paydate = p_paydate + 1.
    
    
        ENDIF.
    
    
      ENDWHILE.
    
    
     
    
    
    ENDFORM.                    " ADD_WORKING_DAYS_RESNONWORK
    
    
    

     


     

    Add n number of working days to date (using SAP personal work schedule)

     

     
    
    The following ABAP code adds n number of WORKING days to a particular date using the employees work schedule (tcode pt63) to calculate working day. 
    
    
    Simply add the below ABAP FORM into you code and call it using the usual PERFORM command:  PERFORM add_working_days_workschedule USING ld_numdays
    
    
                                                                                                      CHANGING gd_date.
    
    
     
    
    
    

     

     

     
    
    *&---------------------------------------------------------------------*
    
    
    *&      Form  ADD_WORKING_DAYS_WORKSCHEDULE
    
    
    *&---------------------------------------------------------------------*
    
    
    *       Add n number of working days to date, using employees work
    
    
    *       schedule to calculate working days
    
    
    *----------------------------------------------------------------------*
    
    
    *      <-- P_DAYS     Number of days to add
    
    
    *      <-- P_PAYDATE  Starting date
    
    
    *----------------------------------------------------------------------*
    
    
     
    
    
    FORM add_working_days_workschedule USING p_days
    
    
                           CHANGING p_paydate TYPE sy-datum.
    
    
      DATA: ld_count  TYPE i,
    
    
            ld_memid(30) TYPE c.
    
    
     
    
    
    * Copied from rptpsh10
    
    
      DATA: psp LIKE ptpsp      OCCURS 0 WITH HEADER LINE.
    
    
      DATA: BEGIN OF hd OCCURS 0,
    
    
            pernr LIKE pernr-pernr,
    
    
            name(40),
    
    
            inv_menge LIKE pakey-seqnr,      "ALV copies only 20 columns!
    
    
            moabw LIKE t001p-moabw,                               "
    
    
            mover LIKE t001p-mover,          "ALV copies only 20 columns!
    
    
            datum LIKE psp-datum,
    
    
            kurzt LIKE t246-kurzt,
    
    
            tprog LIKE psp-tprog,
    
    
            varia LIKE psp-varia,
    
    
             ttext LIKE t550s-ttext,
    
    
            vtart LIKE p2003-vtart,
    
    
             vtext LIKE t556t-vtext,
    
    
            motpr LIKE psp-motpr,
    
    
            sobeg(8),
    
    
            soend(8),
    
    
            stdaz LIKE ptev_rep_h-stdaz,
    
    
            ftkla LIKE psp-ftkla,
    
    
            tagty LIKE psp-tagty,
    
    
             tatxt LIKE t553t-langt,
    
    
            zmodn LIKE psp-zmodn,
    
    
             ptext LIKE t551s-ztext,
    
    
            mofid LIKE t508a-mofid,
    
    
             ftext LIKE thoct-ltext,
    
    
            menge LIKE ptev_rep_h-itanz,
    
    
            alvmarker TYPE rp_xfeld,
    
    
            END OF hd.
    
    
     
    
    
      ld_count = p_days.
    
    
     
    
    
      CONCATENATE sy-uname sy-uzeit INTO ld_memid.
    
    
     
    
    
    * Z version of 'personal work schedule' report, created to export data
    
    
    * to memory rather than display it on screen
    
    
      SUBMIT zrptpsh10_list_to_memory    WITH pnppernr EQ pernr-pernr
    
    
                         WITH pnpbegda EQ p2001-begda
    
    
                         WITH pnpendda EQ p2001-endda
    
    
                         WITH pnpbegps EQ p2001-begda
    
    
                         WITH pnpendps EQ p2001-endda
    
    
                         WITH pnptimed EQ ' '
    
    
                         WITH rdclust  EQ 'X'
    
    
                         WITH p_memid  EQ ld_memid
    
    
                         AND  RETURN.
    
    
     
    
    
    * import datd from memoory
    
    
      IMPORT hd FROM MEMORY ID ld_memid.
    
    
     
    
    
    * Check if days are working days
    
    
      WHILE ld_count GT 0.
    
    
        READ TABLE hd WITH KEY datum = p_paydate.
    
    
        IF hd-tprog NE 'FREE'.  "FREE equals non working day
    
    
          p_paydate = p_paydate + 1.
    
    
          ld_count = ld_count - 1.
    
    
        ELSE.
    
    
          p_paydate = p_paydate + 1.
    
    
        ENDIF.
    
    
      ENDWHILE.
    
    
    ENDFORM.                    " ADD_WORKING_DAYS_WORKSCHEDULE
    
    
    

     


     

    Add n number of working days to date

     

     
    
    The following ABAP code adds/Subtracts n number of months from a particular date. This is a very simple processusing the 'MONTH_PLUS_DETERMINE' function module. If this function module does not exist please see the  more cumbersome solution using SAP FM 'CALCULATE_DATE'.
    
    
     
    
    
    Simply add the below FORM into you ABAP code and call it using the usual PERFORM command:
    
    
                     DATA: ld_date TYPE sy-datum.
    
    
                                   PERFORM calculate_date      using '-4'
    
    
                                                                                changing ld_date.
    
    
     
    
    
                        
    
    
    

     

     

     
    
    *&---------------------------------------------------------------------*
    
    
    *&      Form  CALCULATE_DATE
    
    
    *&---------------------------------------------------------------------*
    
    
    *       Add/Subtract n number of months from a date
    
    
    *----------------------------------------------------------------------*
    
    
    *  -->  p_months  Number of months to add/subtract
    
    
    *  <--  p_date    Start date and result date
    
    
    *----------------------------------------------------------------------*
    
    
     
    
    
    FORM calculate_date USING p_months
    
    
                        CHANGING p_date.
    
    
     
    
    
      DATA: ld_datestor TYPE sy-datum.
    
    
     
    
    
      ld_datestor = p_date.
    
    
     
    
    
      CALL FUNCTION 'MONTH_PLUS_DETERMINE'
    
    
        EXPORTING
    
    
          MONTHS        = p_months
    
    
          OLDDATE       = p_date
    
    
        IMPORTING
    
    
          NEWDATE       = p_date.
    
    
    ENDFORM.                    " CALCULATE_DATE
    
    
     
    
    
    

     


     

    Convert month value of a date to text

     

     
    
    The following ABAP code gets the month value of a particular date (YYYYMMDD) and converts it to its text value. I know its a very crude way of doing it but it does the job and having the ABAP code below provides a quick and easy way to copy and paste it into your SAP program.
    
    
    

     

     

     
    
    *
    
    
     
    
    
    DATA: gd_datetext TYPE string.
    
    
     
    
    
      CASE sy-datum+4(2).
    
    
        WHEN '01'.
    
    
          gd_datetext = 'January'.
    
    
        WHEN '02'.
    
    
          gd_datetext = 'February'.
    
    
        WHEN '03'.
    
    
          gd_datetext = 'March'.
    
    
        WHEN '04'.
    
    
          gd_datetext = 'April'.
    
    
        WHEN '05'.
    
    
          gd_datetext = 'May'.
    
    
        WHEN '06'.
    
    
          gd_datetext = 'June'.
    
    
        WHEN '07'.
    
    
          gd_datetext = 'July'.
    
    
        WHEN '08'.
    
    
          gd_datetext = 'August'.
    
    
        WHEN '09'.
    
    
          gd_datetext = 'September'.
    
    
        WHEN '10'.
    
    
          gd_datetext = 'October'.
    
    
        WHEN '11'.
    
    
          gd_datetext = 'November'.
    
    
        WHEN '12'.
    
    
          gd_datetext = 'December'.
    
    
      ENDCASE.
    
    
     
    
    
      concatenate sy-datum(2) gd_datetext sy-datum+2(2) into gd_datetext
    
    
                                                       separated by space.
    
    
     
    
    
    

     


     

    Date formatting

     

     
    
    The following ABAP code demonstrates a number of ways to format a SAP date value:
    
    
    

     

     

    * Using the WRITE statement
    
    ***************************
    
    
      data: gd_date(10).  "field to store output date
    
    
     
    
    
    * Converts SAP date from 20020901 to 01.09.2002
    
    
      write sy-datum to gd_date dd/mm/yyyy.
    
    
    * Converts SAP date from 20020901 to 01.09.02
    
    
      write sy-datum to gd_date dd/mm/yy.
    
    
     
    
    
    

     

    * Using data manipulation techniques
    
    ************************************
    
    
      data: gd_date(8).  "field to store output date
    
    
     
    
    
    * Converts SAP date from 20010901 to 01092001
    
    
      gd_date(2)   = sy-datum+6(2).
    
    
      gd_date+2(2) = sy-datum+4(2).
    
    
      gd_date+4(4) = sy-datum(4).
    
    
     
    
    
    

     

    * Using Function modules
    
    ************************
    
    
      data: gd_date(8).  "field to store output date
    
    
     
    
    
    * Converts date from 20010901 to 01SEP2001
    
    
      gd_date   = sy-datum.
    
    
      CALL FUNCTION 'CONVERSION_EXIT_IDATE_OUTPUT'
    
    
        EXPORTING
    
    
          input         = gd_date
    
    
        IMPORTING
    
    
          OUTPUT        = gd_date.
    
    
    

     

  • 相关阅读:
    [FAQ] GitHub 开启二次验证之后,如何通过 https clone 项目 ?
    [FAQ] GoLand 需要手动开启代码补全吗 ?
    [FAQ] 夏玉米 按规则查询域名靠谱吗 ?
    [FAQ] Error: com.mysql.jdbc.Driver not loaded. :jdbc_driver_library
    [php-src] Php内核的有趣高频宏
    [php-src] Php扩展开发的琐碎注意点、细节
    [ELK] Docker 运行 Elastic Stack 支持 TLS 的两种简单方式
    [Contract] Solidity 生成随机数方案
    [MySQL] 导入数据库和表的两种方式
    [ELK] 生产环境中 Elasticsearch 的重要配置项
  • 原文地址:https://www.cnblogs.com/omygod/p/1392152.html
Copyright © 2020-2023  润新知