Calculating with Dates, Times, and Timestamps

Objectives

After completing this lesson, you will be able to:

  • Calculate with dates, times, and timestamps

Calculations with Dates, Times, and Timestamps

The built-in ABAP types D and T allow you to represent a day and a time respectively. Dates always have the format YYYYMMDD, regardless of the output format of the user. Times always have the format HHMMSS in 24-hour clock notation, regardless of the output format of the user. Times are accurate to 1 second.

System Information for Time and Date

The class cl_abap_context_info provides methods that you can use to find out current time and date information.

Date and Time Calculations

In ABAP, you can perform direct date calculations such as finding the difference between two dates or adding and subtracting a number of days from a given date. To perform the calculation, ABAP converts the dates into an integer (the number of days since 01.01.0001) and adds or subtracts the relevant values. When performing time calculations, ABAP converts the time into the number of seconds since midnight.

Using Offset and Length with Date Fields

ABAP stores dates in the format YYYYMMDD. You can access individual components of the date using offset and length. To access the month, for example, you need the fifth and sixth characters of the date field. You can do this by specifying var_date+4(2), which tells the system to go four characters into the date, then take the next two characters. The access to year, month, and day is then as follows:

  • Year: var_date(4)
  • Month: var_date+4(2)
  • Day: var_date +6(2)

Using Timestamp Fields

As well as the type D, ABAP has a built-in data type utclong, which represents a timestamp according to ISO-8601. This data type has an accuracy of 100 nanoseconds and follows the format YYYY-MM-DDTHH:MM:SS.sssssssZ, where T is the delimiter between date and time, and Z denotes the time zone Zulu, which stands for UTC.

Try It Out - Calculations with Times, Dates, and Timestamps

  1. Create a new class that implements the interface IF_OO_ADT_CLASSRUN.
  2. Copy the following code into the editor between the METHOD if_oo_adt_classrun~main. and ENDMETHOD. statements:
    Code snippet
    
    DATA timestamp1 TYPE utclong.
    DATA timestamp2 TYPE utclong.
    DATA difference TYPE decfloat34.
    DATA date_user TYPE d.
    DATA time_user TYPE t.
    
    
    timestamp1 = utclong_current( ).
    out->write( |Current UTC time { timestamp1 }| ).
    
    timestamp2 = utclong_add( val = timestamp1 days = 7 ).
    out->write( |Added 7 days to current UTC time { timestamp2 }| ).
    
    difference = utclong_diff( high = timestamp2 low = timestamp1 ).
    out->write( |Difference between timestamps in seconds: { difference }| ).
    
    out->write( |Difference between timestamps in days: { difference / 3600 / 24 }| ).
    
    CONVERT UTCLONG utclong_current( )
    INTO DATE date_user
    TIME time_user
    TIME ZONE cl_abap_context_info=>get_user_time_zone( ).
    
    out->write( |UTC timestamp split into date (type D) and time (type T )| ).
    out->write( |according to the user's time zone (cl_abap_context_info=>get_user_time_zone( ) ).| ).
    out->write( |{ date_user date = user }, { time_user time = user }| ).
    Expand
  3. Activate the class by pressing Ctrl + F3.
  4. Play around with the code to familiarize yourself with the topic.

Log in to track your progress & complete quizzes