Copyright 2022 - BV TallVision IT

Did you know date fields can be added to each other ? A week from now is sy-datum + 7 and there is a function module to check whether the date entered is plausible. Times too ? Times too. 

Check out whether a date is plausible with function module DATE_CHECK_PLAUSIBILITY. There is also a TIME_CHECK_PLAUSIBILITY.

If you need to know how much time was lost in 2 date/times, you can use function module MONTHS_BETWEEN_TWO_DATES or COMPUTE_YEARS_BETWEEN_DATES or a more elaborate DURATION_DETERMINE, which will take a start date, start time and an end date and end time (and a factory calendar as well). The unit of duration (hours, minutes, ...) can also be specified. If you need to work out which end date/time goes with a certain start date/time and duration, you can use END_TIME_DETERMINE. Need to go back in time and calculate a start data/time ? You guessed it: START_TIME_DETERMINE

Turn your date into a textual date ? Call CONVERSION_EXIT_LDATE_OUTPUT with your (internal) date, call it with 20130822 will return 22. August 2013

Check out class CL_BS_PERIOD_TOOLSET_BASICS, which is packed with date calculation utilities.

* Add minutes to a date:
  CL_BS_PERIOD_TOOLSET_BASICS=>ADD_MINUTES_TO_DATE( 
    exporting iv_date = lv_today iv_time = lv_now iv_minutes = 4123  
    importing ev_date = lv_today ev_time = lv_then ).

* Get the first day of the previous month
  lv_last_month = CL_BS_PERIOD_TOOLSET_BASICS=>
GET_FIRST_DAY_PREV_MONTH( IV_DATE = sy-datum ).

Also check out CONV_DATE_TO_EXTERNAL, CONV_DATE_TO_INTERNAL, GET_WEEKDAY_NUMBER, SUBTRACT_MONTHS_FROM_DATE and many many more. Like SD_DATETIME_DIFFERENCE or SD_CALC_DURATION_FROM_DATETIME.

Alternatively, calculations can be done directly in Abap. Simply subtract the times 9:25 and 12:30 like so:

data: lt_t1 type sy-uzeit value '091500', 
      lt_t2 type sy-uzeit value '123000'.

lt_t1 = lt_t2 - lt_t1.
write lt_t1 using edit mask '__:__'. 

This example will show:

03:15

Do make sure you set the times to a time including seconds, otherwise this calculation won't work. You can also us a calculation enabling type case for times like this:

data: begin of lw_timecaster,
        h type n length 2,
        m type n length 2,
        s type n length 2,  
      end of lw_timecaster, 
      lv_hours type p length 5 decimals 2. 

move lt_t1 to lw_timecaster. 
lv_hours = lw_timecaster-h + ( lw_timecaster-m / 60 ). 
write lv_hours decimals 2. 

For which the output would show as

3.25