Dates, Times and Built-in Functions

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

Almost every business program deals with time: when a claim was filed, how many days ago that was, and whether a deadline has passed. This page explains how ABAP stores a calendar date, a time of day and a precise moment in time, how it calculates with them, and how a moment is shown in the time zone of a user. It also gives an overview of the built-in functions, small ready-made calculations that you write inside an expression, with a closer look at the numeric ones. It is written against Standard ABAP release 8.16 and ABAP for Cloud Development release 9.20 (2608). The types and functions apply to ABAP Cloud and to classic, Standard ABAP only, code on premise; where a classic habit, such as reading the date from a system field, is not allowed in ABAP Cloud, the text says so. After reading it you can work out the number of days between two dates, add time to a time stamp and round an amount correctly. The examples use the Expense claims running example (the table ZEXP_CLAIM) and simple generic values.

Before you read

Key terms

  • Time stamp: a single value that names one exact moment, down to a fraction of a second.

  • UTCLONG: the built-in type that holds a time stamp in Coordinated Universal Time with a resolution of 100 nanoseconds.

  • Time zone: a region whose clocks show the same local time, written in ABAP as a short key such as EST or UTC.

  • Built-in function: a function that is part of the language itself and is called inside an expression, for example abs( x ).

  • Functional method call: a call of a method that returns one value, written where a value is expected, such as cl_abap_context_info=>get_system_date( ).

  • Rounding mode: the rule that decides in which direction a value that lies exactly halfway is rounded.

Dates and times as types

ABAP has two small types for the calendar and the clock. Type d holds a date as eight characters in the form YYYYMMDD. Type t holds a time of day as six characters in the form HHMMSS. Both are character-like, so 20261113 is the 13th of November 2026 and 143000 is half past two in the afternoon. The initial value of a date is 00000000 and of a time is 000000. Because the text form is fixed, a date can be compared with < and > and sorted as it is, and the program never depends on the date format the user prefers; formatting for display is a separate step.

When a date takes part in a calculation, ABAP converts it to a number of days, and a time to a number of seconds. This gives two useful rules. A date plus or minus a whole number is another date, so adding 30 to a date moves it 30 days later. The difference of two dates is a number of days. The same holds for times, in seconds.

This listing runs in the local playground. Written as a report that ends with a WRITE statement for the three results, it prints 20261201, 30 and 6300: the due date, the days between the two dates and the seconds between the two times (the playground prints the values one after another without separators).

REPORT zp_d.

DATA claim_date TYPE d VALUE '20261101'.
DATA due_date   TYPE d.
DATA wait_days  TYPE i.
DATA start_time TYPE t VALUE '083000'.
DATA end_time   TYPE t VALUE '101500'.
DATA seconds    TYPE i.

due_date  = claim_date + 30.
wait_days = due_date - claim_date.
seconds   = end_time - start_time.

WRITE: / due_date, wait_days, seconds.

The parts of a date can also be read with offset and length, such as claim_date+4(2) for the month, but this works on the text form. Calculations with dates and times are only meaningful when the content is a valid date or time, so check a value that comes from outside the program first.

The current date and time

The classic way to get today’s date and the current time is the system fields sy-datum and sy-uzeit. They hold values that belong to the system, and ABAP for Cloud Development should not use them, nor the related fields sy-datlo and sy-timlo. In ABAP Cloud use the class cl_abap_context_info instead. Its methods get_system_date( ) and get_system_time( ) return the date and the time, and get_user_time_zone( ) returns the time zone of the current user and can raise the exception cx_abap_context_info_error. A method call that returns one value can be written where a value is expected, which is why the class can be used directly inside an expression.

This listing requires an SAP system; not run here.

DATA(today) = cl_abap_context_info=>get_system_date( ).
DATA(now)   = cl_abap_context_info=>get_system_time( ).

TRY.
    DATA(zone) = cl_abap_context_info=>get_user_time_zone( ).
  CATCH cx_abap_context_info_error.
ENDTRY.

The system date and time does not depend on the user. For a business date that must read the same for every user, such as the posting date of a claim, the system date is normally the right choice. For a message that tells a person when something happened, convert a time stamp into the time zone of that person, as shown in the next sections.

Time stamps and the utclong type

A date and a time are only half of a moment: the same clock reading means a different moment in Berlin and in Lima. A time stamp removes the ambiguity by always using Coordinated Universal Time, written UTC, which is the same everywhere. ABAP offers two forms of time stamp.

  • The built-in type utclong holds a time stamp with a resolution of 100 nanoseconds. Its text form is YYYY-MM-DD hh:mm:ss.fffffff.

  • The older time stamps are packed numbers: the data element timestamp has the form YYYYMMDDhhmmss and timestampl adds seven decimals for the fraction. The statement GET TIME STAMP FIELD fills one, and the class cl_abap_tstmp calculates with them. They are still found in many tables and interfaces.

For utclong there are built-in functions: utclong_current( ) returns the current time stamp, utclong_add( ) adds days, hours, minutes or seconds to one (there are no parameters for months or years), and utclong_diff( ) returns the exact difference between two time stamps in seconds, as a decfloat34 value that is negative when the first time stamp is the earlier one.

This listing requires an SAP system; not run here.

DATA(started) = utclong_current( ).

"... the work happens here ...

DATA(finished) = utclong_current( ).
DATA(elapsed)  = utclong_diff( high = finished low = started ).

"a deadline one day and six hours after the start
DATA(deadline) = utclong_add( val = started days = 1 hours = 6 ).

Time zones

A utclong time stamp is the same for everyone, but a person reads a local date and time. The statement CONVERT UTCLONG turns a time stamp into a date and a time in a given time zone, and CONVERT DATE ... TIME ... INTO UTCLONG does the opposite. The older time stamps use CONVERT TIME STAMP and CONVERT DATE ... INTO TIME STAMP in the same way. The time zone is a short key such as EST or UTC; it must exist in the table of time zones of the system, otherwise a catchable exception of the class cx_sy_conversion_no_date_time is raised. The key of the current user comes from get_user_time_zone( ). The conversion follows the rules of the time zone, which is also what keeps the change between summer and winter time correct.

This listing requires an SAP system; not run here.

DATA claim_ts TYPE utclong.
DATA local_date TYPE d.
DATA local_time TYPE t.

claim_ts = utclong_current( ).

CONVERT UTCLONG claim_ts
        INTO DATE local_date
             TIME local_time
             TIME ZONE cl_abap_context_info=>get_user_time_zone( ).

Source: CONVERT UTCLONG

The diagram shows how the forms relate. The time stamp in the middle is the one stored and compared; the date and time on the right are what a person reads.

flowchart LR A[utclong
UTC time stamp] -- CONVERT UTCLONG
with time zone --> B[date d and time t
local clock reading] B -- CONVERT DATE ... INTO UTCLONG
with time zone --> A C[timestamp / timestampl
packed number] -- CONVERT TIME STAMP
with time zone --> B A -- utclong_add / utclong_diff --> D[later time stamp
or seconds]

Example: days between the claim date and today

An expense claim has a claim date. The finance team wants to know how many days a claim has been waiting, and to flag the ones that wait more than 14 days. Both values are dates, so the subtraction gives a number of days.

This listing requires an SAP system; not run here.

DATA claim_date TYPE d.

SELECT SINGLE claim_date
  FROM zexp_claim
  WHERE claim_id = '0000004711'
  INTO @claim_date.

IF sy-subrc = 0.
  DATA(days_waiting) = cl_abap_context_info=>get_system_date( ) - claim_date.
  IF days_waiting > 14.
    DATA(message) = |Claim waits { days_waiting } days: escalate|.
  ENDIF.
ENDIF.

The subtraction of two dates is calculated with whole days, so days_waiting holds a number of days. What to do with message, for example write it to an application log or return it from the method, depends on the application. In classic code the same calculation can use the system field, as in sy-datum - claim_date.

Built-in functions in general

A built-in function is a name followed by parentheses that returns a value and can be written wherever an operand fits. Functions that take several values use named parameters, such as round( val = x dec = 2 ); a function with one main value also accepts it as a single unnamed argument, such as abs( x ), as the official examples do. The functions fall into groups.

Group Examples and purpose

Numeric

abs, sign, ceil, floor, trunc, frac, round, rescale, nmax, nmin, ipow, sqrt, exp, log, log10, and the trigonometric functions such as sin and cos

Description

strlen, xstrlen, lines, charlen, numofchar: they report a property of an operand, such as its length or the number of rows of an internal table

Character string

condense, concat_lines_of, replace, substring, to_upper, to_lower, shift_left, find, match and more, covered in the page on strings

Time stamp

utclong_current, utclong_add, utclong_diff

Logical

xsdbool and boolc, which turn a condition into a flag

The rest of this page looks at the numeric group, because it is the one used in every amount calculation.

Numeric functions

Some numeric functions change an amount to a whole number or a given number of decimals. abs removes the sign and sign returns -1, 0 or 1. ceil returns the smallest whole number that is not less than the value, floor the largest that is not greater, trunc cuts off the decimals and frac returns only the decimals. The difference shows with a negative value: the results for -2.5 are -2 for ceil, -3 for floor, -2 for trunc and -0.5 for frac.

The function round( ) rounds a decimal floating point number to a number of decimals given with dec, or to a precision given with prec; one of the two is required. The result always has the type decfloat34. The optional mode chooses the rounding rule from the constants round_... of the class cl_abap_math; if no mode is given, commercial rounding is used, which rounds a value that lies exactly halfway away from zero. The function rescale( ) is similar, with dec for the scale or prec for the precision, and it rounds the input if that is needed. The functions nmax( ) and nmin( ) return the greatest or smallest of two to nine numeric arguments, and ipow( ) raises a number to a whole power.

This listing requires an SAP system; not run here. Several of these functions give a different result in the local playground, so the values below follow the official documentation instead.

DATA total TYPE decfloat34 VALUE '125.455'.
DATA limit TYPE decfloat34 VALUE '100.00'.

DATA(rounded)   = round( val = total dec = 2 ).
DATA(ceiling)   = ceil( total ).
DATA(floored)   = floor( total ).
DATA(whole)     = trunc( total ).
DATA(fraction)  = frac( total ).
DATA(biggest)   = nmax( val1 = total val2 = limit ).
DATA(squared)   = ipow( base = 12 exp = 2 ).

With the values above, rounded is 125.46 (the halfway case 125.455 is rounded away from zero), ceiling is 126, floored and whole are 125, fraction is 0.455, biggest is 125.455 and squared is 144.

Older material you may meet

Older programs and books show a different style of working with time and numbers.

  • The date was read from sy-datum and the time from sy-uzeit everywhere. This still works in classic code, but ABAP for Cloud Development should not use these fields; the class cl_abap_context_info replaces them.

  • Time stamps were packed numbers of type timestamp or timestampl set with GET TIME STAMP FIELD and changed with the class cl_abap_tstmp. The type utclong and its functions were added later and replace most of that code in new programs.

  • Date arithmetic often called function modules. Today a plain addition or subtraction of dates covers days, as shown above.

  • Rounding was done by assigning to a variable with fewer decimals, which depends on the conversion rule. The function round( ) states the intention and its rounding rule.

  • Results of a function were first stored in a variable with DATA on a separate line; inline declarations such as DATA(rounded) = round( ... ) are the current style.

Summary

  • A date is type d (YYYYMMDD) and a time is type t (HHMMSS); adding a number to a date moves it by days and subtracting two dates gives a number of days.

  • In ABAP Cloud, read the current date and time and the user time zone with cl_abap_context_info; the system fields sy-datum and sy-uzeit belong to classic code.

  • A time stamp names one moment in UTC. The type utclong has a resolution of 100 nanoseconds and works with utclong_current( ), utclong_add( ) and utclong_diff( ).

  • Convert a time stamp to a local date and time with CONVERT UTCLONG and a time zone; do not add hours by hand.

  • Built-in functions are used inside expressions. The numeric ones round, ceil, floor, trunc, frac, nmax and nmin cover most amount calculations; round works on decfloat34 and uses commercial rounding by default.