Date and Time plays key role in application programming. Most of the reports identifies by using the date and time they generated or date & time written on it.
ABAP indirectly references Gregorian calendar and produces the output in the same format. The output can be converted to country specific format if required.
A day points to the specific day in the calendar year. Time is specific to precise the second or minute of the specific day.
ABAP supports 24 hour formats and save the time in the same format. ABAP supports two build-in data types to work with Date and Time. Those are –
- Date Data Type (D)
- Time Data Type (T)
Data type Keyword | Length | Range | Description |
---|---|---|---|
D | 8 characters | 8 characters | Used for internal representation of YYYYMMDD using Georgian calendar date. Can set default format in profile. Several output formats supported. Supports arithmetic operations. |
T | 6 characters | 6 characters | Used for time. Format is HHMMSS. Several output formats supported. Supports arithmetic operations. |
Working with Date –
When we are working on dates, we may need to perform various computations like date comparisons, date validations, date range validations, date increments, date decrements and so on.
However as discussed earlier, the Date built-in data type is of type character. So normally we can’t able to perform arithmetic operations on character data.
However, ABAP runtime environment supports the basic operations on Date by internally converts it to numeric and performs the operations on it.
Example –
Below example shows date operations performed in ABAP application program.
Code –
*&---------------------------------------------------------------------*
*& Report Z_DATE
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*
REPORT Z_DATE.
*Declaring required date fields
DATA: W_DATE1 TYPE D,
W_DATE2 TYPE D.
* Assigning system date to W_TIME1
W_DATE1 = SY-DATUM.
WRITE: / 'Current Date : ', W_DATE1 DD/MM/YYYY.
* adding 365 days (1 year) to W_TIME1
W_DATE2 = W_DATE1 + 365.
WRITE: / 'Date after 1 year : ', W_DATE2 DD/MM/YYYY.
*Comparing date1 and date2
IF W_DATE1 > W_DATE2.
WRITE: W_DATE1 DD/MM/YYYY,' is greater than ',W_DATE2 DD/MM/YYYY.
ELSE.
WRITE: / W_DATE2 DD/MM/YYYY,' is greater than ',W_DATE1 DD/MM/YYYY.
ENDIF.
Output –
Explaining Example –
In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.
W_DATE1 = SY-DATUM, Assigns system date to W_DATE1. W_DATE1 DD/MM/YYYY converts the date to DD/MM/YYYY format.
W_DATE2 = W_DATE1 + 365, adds 365 days to W_DATE1 and stores the result i W_DATE2. IF W_DATE1 > W_DATE2, compares the W_DATE1 and W_DATE2 and display the messages accroding to the condition.
Working with Time –
When you are working on Time, we may need to perform various computation like time comparisons, time validations, time range validations, time increments, time decrements and so on.
But as discussed earlier, the time built-in data type is of type character. So we can’t able to perform arithmetic operations on character data.
However, ABAP runtime environment supports the basic operations on time by internally converts it to numeric and performs the operations on it.
Example –
Below example shows time operations performed in ABAP application program.
Code –
*&---------------------------------------------------------------------*
*& Report Z_TIME
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*
REPORT Z_TIME.
* Declaring variables of data type T.
DATA: W_TIME1 TYPE T,
W_TIME2 TYPE T.
* Assigning system time to W_TIME1
W_TIME1 = SY-UZEIT.
WRITE /(60) W_TIME1 USING EDIT MASK 'The current time is __:__:__'.
* Adding 10 minutes (10*60= 600 seconds)
W_TIME2 = W_TIME1 + 600.
WRITE:/(60) W_TIME2 USING EDIT MASK 'Time after 10 minutes is __:__:__'.
* Comparing time1 and time2
IF W_TIME1 > W_TIME2.
WRITE: / W_TIME1,' is greater than ',W_TIME2.
ELSE.
WRITE: / W_TIME2,' is greater than ',W_TIME1.
ENDIF.
Output –
Explaining Example –
In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.
W_TIME1 = SY-UZEIT, Assigns system time to W_TIME1. W_TIME1 USING EDIT MASK converts the time to mask format provided __:__:__.
W_TIME2 = W_TIME1 + 600, adds 10 minutes to W_TIME1 and stores the result i W_TIME2. IF W_TIME1 > W_TIME2, , compares the W_TIME1 and W_TIME2 and display the messages accroding to the condition.