SAP ABAP - Date & Time


Advertisements


ABAP implicitly references the Gregorian calendar, valid across most of the world. We can convert the output to country specific calendars. A date is a time specified to a precise day, week or month with respect to a calendar. A time is specified to a precise second or minute with respect to a day. ABAP always saves time in 24-hour format. The output can have a country specific format. Dates and time are usually interpreted as local dates that are valid in the current time zone.

ABAP provides two built-in types to work with dates and time −

  • D data type
  • T data type

Following is the basic format −

DATA: date TYPE D, 
      time TYPE T.  
	
DATA: year TYPE I, 
month TYPE I,  
day TYPE I, 
hour TYPE I,  
minute TYPE I, 
second TYPE I.

Both of these types are fixed-length character types that have the form YYYYMMDD and HHMMSS, respectively.

Timestamps

In addition to these built-in types, the other two types TIMESTAMP and TIMESTAMPL are being used in many standard application tables to store a timestamp in the UTC format. Following table shows the basic date and time types available in ABAP.

S.No. Data Type & Description
1

D

A built-in fixed-length date type of the form YYYYMMDD. For example, the value 20100913 represents the date September 13, 2010.

2

T

A built-in fixed-length time type of the form HHMMSS. For example, the value 102305 represents time 10:23:05 AM.

3

TIMESTAMP (Type P – Length 8 No decimals)

This type is used to represent short timestamps in YYYYMMDDhhmmss form. For instance, the value 20100913102305 represents the date September 13, 2010 at 10:23:05 AM.

4

TIMESTAMPL (Type P - Length 11 Decimals 7)

TIMESTAMPL represents long timestamps in YYYYMMDDhhmmss,mmmuuun form. Here the additional digits ‘mmmuuun’ represent the fractions of a second.

Current Date and Time

The following code snippets retrieve the current system date and time.

REPORT YR_SEP_15. 
DATA: date_1 TYPE D. 

date_1 = SY-DATUM. 
Write: / 'Present Date is:', date_1 DD/MM/YYYY. 

date_1 = date_1 + 06. 
Write: / 'Date after 6 Days is:', date_1 DD/MM/YYYY.

The above code produces the following output −

Present Date is: 21.09.2015 
Date after 6 Days is: 27.09.2015

The variable date_1 is assigned the value of the current system date SY-DATUM. Next, we increment the date value by 6. In terms of a date calculation in ABAP, this implies that we’re increasing the day component of the date object by 6 days. The ABAP runtime environment is smart enough to roll over the date value whenever it reaches the end of a month.

Time calculations work similar to date calculations. The following code increments the current system time by 75 seconds using basic time arithmetic.

REPORT YR_SEP_15. 
DATA: time_1 TYPE T. 
      time_1 = SY-UZEIT. 

Write /(60) time_1 USING EDIT MASK 
'Now the Time is: __:__:__'. 
time_1 = time_1 + 75. 

Write /(60) time_1 USING EDIT MASK 
'A Minute and a Quarter from Now, it is: __:__:__'.

The above code produces the following output −

Now the Time is 11:45:05 
A Minute and a Quarter from Now, it is: 11:46:20

Working with Timestamps

You can retrieve the current system time and store it in a timestamp variable using GET TIME STAMP as shown in the following code. The GET TIME STAMP statement stores the timestamp in a long-hand or a short-hand format according to the type of the timestamp data object used. Timestamp value is encoded using the UTC standard.

REPORT YR_SEP_12. 
DATA: stamp_1 TYPE TIMESTAMP,
 
stamp_2 TYPE TIMESTAMPL. 
GET TIME STAMP FIELD stamp_1. 
Write: / 'The short time stamp is:', stamp_1 

TIME ZONE SY-ZONLO. 
GET TIME STAMP FIELD stamp_2. 
Write: / 'The long time stamp is:', stamp_2 
TIME ZONE SY-ZONLO.

The above code produces the following output −

The short time stamp is: 18.09.2015 11:19:40 
The long time stamp is: 18.09.2015 11:19:40,9370000

In the above example, we are displaying the timestamp using the TIME ZONE addition of the WRITE statement. This addition formats the output of the timestamp according to the rules for the time zone specified. The system field SY-ZONLO is used to display the local time zone configured in the user’s preferences.



Advertisements
E-Books Store