6 Columns with Active Background
6 Columns with Active Background
Active List Item Background

24CONVERSION2611 – Time and Date Handling

In SAP ABAP, date and time handling plays a vital role in conversions, calculations and processing. ABAP provides specific data types, functions and techniques to handle date and time efficiently.

1. Common Data Types for Date and Time

ABAP has dedicated data types to store and manipulate date and time:

  • D (Date): Stores dates in the format YYYYMMDD (8 characters).
  • T (Time): Stores time in the format HHMMSS (6 characters).
  • TIMESTAMP (Time Stamp): Stores date and time in the format YYYYMMDDHHMMSS.

For custom handling, you can use CHAR or STRING types, but these require manual formatting.

2. Converting Dates and Times

a. Converting Between Formats

From D to CHAR:

DATA: lv_date TYPE d VALUE ‘20250128’,

      lv_date_char TYPE char10.

WRITE lv_date TO lv_date_char DD/MM/YYYY.

” Output: ’28/01/2025′ in lv_date_char

 

From CHAR to D:

DATA: lv_date TYPE d,

      lv_date_char TYPE char8 VALUE ‘20250128’.

lv_date = lv_date_char.

 

 

b. Converting Between D and TIMESTAMP:

From D and T to TIMESTAMP:

DATA : lv_date TYPE d VALUE ‘20250128’,

      lv_time TYPE t VALUE ‘153045’,

      lv_timestamp TYPE timestamp.

               CONCATENATE lv_date lv_time INTO lv_timestamp.

” Output: ‘20250128153045’ in lv_timestamp

From TIMESTAMP to D and T:

DATA : lv_timestamp TYPE timestamp VALUE ‘20250128153045’,  ” Timestamp in format YYYYMMDDHHMMSS

      lv_date TYPE d,

      lv_time TYPE t.

               lv_date = lv_timestamp(8).  ” First 8 characters

lv_time = lv_timestamp+8(6).  ” Next 6 characters

 

3. Date and Time Arithmetic

Adding/Subtracting Days to a Date:

DATA : lv_date TYPE d VALUE ‘20250128’,

      lv_new_date TYPE d.

lv_new_date = lv_date + 10.  ” Add 10 days

lv_new_date = lv_date – 5.   ” Subtract 5 days

Calculating Differences:

DATA : lv_date1 TYPE d VALUE ‘20250128’,

      lv_date2 TYPE d VALUE ‘20250120’,

      lv_diff TYPE i.

                 lv_diff = lv_date1 – lv_date2.

” Output: lv_diff = 8 (difference in days)

Adding/Subtracting Time:

You need to manually handle overflows when dealing with the T type.

 

4. Built-in Date and Time Functions

ABAP provides several functions for date and time handling:

  • CONVERT_DATE_TO_EXTERNAL: Converts an internal date to a formatted date.
  • CONVERT_TIME_TO_EXTERNAL: Converts an internal time to a formatted time.
  • CONVERT_DATE_TO_INTERNAL: Converts a formatted date to an internal date.
  • CONVERT_TIME_TO_INTERNAL: Converts a formatted time to an internal time.
  • CONVERT_TIMESTAMP_TO_DATE_TIME: Converts a timestamp to a date and time.
  • CONVERT_DATE_TIME_TO_TIMESTAMP: Converts a date and time to a timestamp.

5. Formatting Dates and Times

Using the WRITE Statement:

DATA : lv_date TYPE d VALUE ‘20250128’,

      lv_formatted TYPE string.

WRITE lv_date TO lv_formatted DD/MM/YYYY.

” Output: ’28/01/2025′

Using FORMAT Options:

DATA : lv_time TYPE t VALUE ‘153045’.

WRITE : / lv_time USING EDIT MASK ‘__:__:__’.

” Output: ’15:30:45′

 

6. Handling Time Zones

For global applications, consider time zones:

    • Use GET TIME STAMP to get UTC timestamps.
    • Use CONVERT TIME STAMP to convert between time zones.

DATA : lv_utc_timestamp TYPE timestamp,

      lv_local_timestamp TYPE timestamp.

GET TIME STAMP FIELD lv_utc_timestamp.

CALL FUNCTION ‘CONVERT_TIME_STAMP’

  EXPORTING

    timestamp = lv_utc_timestamp

    time_zone = ‘CET’

  IMPORTING

    local_timestamp = lv_local_timestamp.

” Converts UTC to CET

7.Validating Dates and Times

Use IS_DATE and IS_TIME functions to validate inputs:

DATA : lv_date TYPE string VALUE ‘20250128’.

IF cl_abap_date_utilities=>is_date( lv_date ) = abap_true.

               WRITE ‘Valid Date’.

               ELSE.

               WRITE ‘Invalid Date’.

ENDIF.

8. Common Use Cases

  • Converting timestamps to user-friendly formats.
  • Adding days or months to a given date.
  • Calculating time differences.
  • Formatting dates and times for display.

Author : Aniket Pawar, 9373518385  

24CONVERSION2611 – Time and Date Handling

In SAP ABAP, date and time handling plays a vital role in conversions, calculations and processing. ABAP provides specific data types, functions and techniques to handle date and time efficiently.

1. Common Data Types for Date and Time

ABAP has dedicated data types to store and manipulate date and time:

  • D (Date): Stores dates in the format YYYYMMDD (8 characters).
  • T (Time): Stores time in the format HHMMSS (6 characters).
  • TIMESTAMP (Time Stamp): Stores date and time in the format YYYYMMDDHHMMSS.

For custom handling, you can use CHAR or STRING types, but these require manual formatting.

2. Converting Dates and Times

a. Converting Between Formats

From D to CHAR:

DATA: lv_date TYPE d VALUE ‘20250128’,

      lv_date_char TYPE char10.

WRITE lv_date TO lv_date_char DD/MM/YYYY.

” Output: ’28/01/2025′ in lv_date_char

 

From CHAR to D:

DATA: lv_date TYPE d,

      lv_date_char TYPE char8 VALUE ‘20250128’.

lv_date = lv_date_char.

 

 

b. Converting Between D and TIMESTAMP:

From D and T to TIMESTAMP:

DATA : lv_date TYPE d VALUE ‘20250128’,

      lv_time TYPE t VALUE ‘153045’,

      lv_timestamp TYPE timestamp.

               CONCATENATE lv_date lv_time INTO lv_timestamp.

” Output: ‘20250128153045’ in lv_timestamp

From TIMESTAMP to D and T:

DATA : lv_timestamp TYPE timestamp VALUE ‘20250128153045’,  ” Timestamp in format YYYYMMDDHHMMSS

      lv_date TYPE d,

      lv_time TYPE t.

               lv_date = lv_timestamp(8).  ” First 8 characters

lv_time = lv_timestamp+8(6).  ” Next 6 characters

 

3. Date and Time Arithmetic

Adding/Subtracting Days to a Date:

DATA : lv_date TYPE d VALUE ‘20250128’,

      lv_new_date TYPE d.

lv_new_date = lv_date + 10.  ” Add 10 days

lv_new_date = lv_date – 5.   ” Subtract 5 days

Calculating Differences:

DATA : lv_date1 TYPE d VALUE ‘20250128’,

      lv_date2 TYPE d VALUE ‘20250120’,

      lv_diff TYPE i.

                 lv_diff = lv_date1 – lv_date2.

” Output: lv_diff = 8 (difference in days)

Adding/Subtracting Time:

You need to manually handle overflows when dealing with the T type.

 

4. Built-in Date and Time Functions

ABAP provides several functions for date and time handling:

  • CONVERT_DATE_TO_EXTERNAL: Converts an internal date to a formatted date.
  • CONVERT_TIME_TO_EXTERNAL: Converts an internal time to a formatted time.
  • CONVERT_DATE_TO_INTERNAL: Converts a formatted date to an internal date.
  • CONVERT_TIME_TO_INTERNAL: Converts a formatted time to an internal time.
  • CONVERT_TIMESTAMP_TO_DATE_TIME: Converts a timestamp to a date and time.
  • CONVERT_DATE_TIME_TO_TIMESTAMP: Converts a date and time to a timestamp.

5. Formatting Dates and Times

Using the WRITE Statement:

DATA : lv_date TYPE d VALUE ‘20250128’,

      lv_formatted TYPE string.

WRITE lv_date TO lv_formatted DD/MM/YYYY.

” Output: ’28/01/2025′

Using FORMAT Options:

DATA : lv_time TYPE t VALUE ‘153045’.

WRITE : / lv_time USING EDIT MASK ‘__:__:__’.

” Output: ’15:30:45′

 

6. Handling Time Zones

For global applications, consider time zones:

    • Use GET TIME STAMP to get UTC timestamps.
    • Use CONVERT TIME STAMP to convert between time zones.

DATA : lv_utc_timestamp TYPE timestamp,

      lv_local_timestamp TYPE timestamp.

GET TIME STAMP FIELD lv_utc_timestamp.

CALL FUNCTION ‘CONVERT_TIME_STAMP’

  EXPORTING

    timestamp = lv_utc_timestamp

    time_zone = ‘CET’

  IMPORTING

    local_timestamp = lv_local_timestamp.

” Converts UTC to CET

7.Validating Dates and Times

Use IS_DATE and IS_TIME functions to validate inputs:

DATA : lv_date TYPE string VALUE ‘20250128’.

IF cl_abap_date_utilities=>is_date( lv_date ) = abap_true.

               WRITE ‘Valid Date’.

               ELSE.

               WRITE ‘Invalid Date’.

ENDIF.

8. Common Use Cases

  • Converting timestamps to user-friendly formats.
  • Adding days or months to a given date.
  • Calculating time differences.
  • Formatting dates and times for display.

Author : Aniket Pawar, 9373518385