Q: How to find duration in year and months from date field in internal tables without API's in ABAP? So, I have 2 internal tables, with a date of joining field in the 1st table and a service years field in the other table; I have added the data to the tables manually.
I have used the following logic to find years but it seems incomplete:
    TYPES : BEGIN OF s_empdetails,
              empid       TYPE n,
              fname(20)       TYPE c,
              doj         TYPE dats,
             END OF s_empdetails.
    
    TYPES : BEGIN OF s_details,
              empid      TYPE n,
              serv_yrs   TYPE dats,
            END OF s_details.
    
    DATA : gt_header  TYPE TABLE OF s_empdetails WITH HEADER LINE,
           gwa_header TYPE s_empdetails.
    
    DATA :gt_result  TYPE TABLE OF s_details,
          gwa_result TYPE s_details.

DATA : gv_date     LIKE sy-datum.

gv_date = sy-datum.
    
    gwa_header-empid = 10002.
    gwa_header-name = 'Aditya'.
    gwa_header-doj = '20200205'.
    
    APPEND gwa_header TO gt_header.
    CLEAR gwa_header.
    
    gwa_header-empid = 10003.
    gwa_header-name = 'Sumit'.
    gwa_header-doj = '20170701'.
    
    APPEND gwa_header TO gt_header.
    CLEAR gwa_header.
    
    LOOP AT gt_header INTO gwa_header.
      gwa_result-empid = gwa_header-empid.
      gwa_result-name = gwa_header-name.
      
      APPEND gwa_result TO gt_result.
      ENDLOOP.

I used the following logic to get the number of years but it doesn't give accurate years, as a person joining in January will still get 1 year service on his name. So, how can I get years and months as well here.
LOOP AT gt_result ASSIGNING FIELD-SYMBOL(<gwa_result>).

  READ TABLE gt_header ASSIGNING <gwa_header> WITH KEY empid = <gwa_result>-empid.

  <gwa_result>-serv_yrs = gv_date+0(4) - <gwa_header>-doj+0(4).

ENDLOOP.


A: Working with dates and times is more difficult than they seem at first. So it's usually advisable to leave date-arithmetics to a library. Unfortunately I am not aware of any standard library for date and time handling which is available on every ABAP system. There are just some module-specific libraries like the class CL_RECA_DATE in the module RE-FX or the function module COMPUTE_YEARS_BETWEEN_DATES which is part of the HR module.
Unfortunately we don't know what modules you have installed on your system, so we don't know which of those are available to you. Which means we need a pure ABAP solution.
A solution which does not handle leap-years correctly but might be good enough for some situations might be this:
DATA(years_between) = ( gv_date - <gwa_header>-doj ) / '365.0'.

Subtracting one TYPE d variable from another gives you the number of days between those two dates. Dividing that by 365 would convert those to a number of years if every year had 365 days*. Not stating a type for this inline declaration of years_between results in type I being determined, which will result in the year being rounded to the nearest integer. If you need the exact fraction of years, you can get that by stating the type of years_between as a type which can handle more accuracy. Like a floating point value, for example:
DATA years_between TYPE f.
years_between = ( sy-datum - date1 ) / '365.0'.

Note that TYPE dats like in your code snippet above is not going to work. It will give you meaningless results. That type represents a specific date, not a timespan between two dates. So don't try to use it in that context.

*Unfortunately not all years have 365 days. If you need to handle leap-years at all and if so how you would handle them depends on the exact use-case for the numbers you are calculating here. So I am going to leave that as an exercise to the reader :)
