Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Calculate difference between two dates

Former Member
0 Kudos
12,366

Hi,

   How to calculate the difference between two dates in abap ,

in which one is the current day date calculated by sy-datum and the data type of other date is DATS ?

9 REPLIES 9

0 Kudos
2,605

Hi Somya,

you can utilize the function like SD_DATETIME_DIFFERENCE for the same.

Thanks & Regards,

Jayraj Joshi

Former Member
0 Kudos
2,605

Hello Somya,

No issues my friend. It will work fine, just take 2 variables assign one to sy-datum and another to dats. You will get the result.

Is there any other problem you are facing, please let know .

Thanks & Regards,

Varun Kumar Sahu

raymond_giuseppi
Active Contributor
0 Kudos
2,605

Did you try SUBTRACT or "-" operator (between two date field it will give an integer): number of days ?

(Actually look for conversion of data type (*), as the operator require numeric data, it will convert date to numeric, and as you can read a date is converted to the number of day since  01.01.0001, and vice-versa)

Regards,

Raymond

(*) Conversion Table for Source Field Type d

prakashjasti
Contributor
0 Kudos
2,605

Hi,

you can directly use - operator, after using conversion routine.

Regards,

Prakash.

Prakash J

Former Member
0 Kudos
2,605

Data type of sy-datum is DATS. So, there should be no issue in addition & subtraction in your 2 variables.

Please correct me if I am wrong.

Thanks,

Former Member
0 Kudos
2,605

Hi,

Use the function module "  HR_HK_DIFF_BT_2_DATE".

Here is the implementation:

   CALL FUNCTION 'HR_HK_DIFF_BT_2_DATES'
   EXPORTING
     date1                         =  sy-datum

     date2                         =  date2
     output_format                 = '02'
  IMPORTING
*   YEARS                         =
*   MONTHS                        =
    days                           = var_date " store no of days in between dates+1
  EXCEPTIONS
    invalid_dates_specified       = 1
    OTHERS                        = 2
           .

var_date = (var_date -1)"store no of days in between dates

Regards,

Priyanka

Former Member
0 Kudos
2,605

Hi,

Both sy-datum and dats have same technical attributes when you compared with domain.

Calculations with dates values are possible.

Example:

former_member189849
Contributor
0 Kudos
2,605

Here is the sample code just copy it and analyze i hope u can understand .

PARAMETERS: p_date1 TYPE sy-datum,

             p_date2 TYPE sy-datum.

 

DATA: w_a TYPE i,

       w_b TYPE i,

       w_c TYPE i.

  

IF p_date1 > p_date2.

w_a = p_date1 - p_date2.

w_b = w_a / 7.

w_c = w_a mod 7.

WRITE: 'total',w_a,'days', 'OR ', w_b,'weeks',w_c,'days' .

ENDIF.

Regards

Mahesh

Former Member
0 Kudos
2,605

Hi Sowmya,

Check this below Program.

I think  it wil satisfy ur requirement.

   PARAMETERS : p_month TYPE sy-datum,
              p_year TYPE dats.

data: i_date type p.

CALL FUNCTION '/SDF/CMO_DATETIME_DIFFERENCE'
EXPORTING
   DATE1                  = p_month
*   TIME1                  =
   DATE2                  = p_year
*   TIME2                  =
IMPORTING
   DATEDIFF               = i_date
*   TIMEDIFF               =
*   EARLIEST               =
EXCEPTIONS
   INVALID_DATETIME       = 1
   OTHERS                 = 2
          .
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

in the variable i_date will give you the exact output.