In SAP ABAP, you can convert a date (sy-datum
) to a timestamp (TIMESTAMP
format: YYYYMMDDHHMMSS)` using different methods, depending on whether you need a full timestamp with time or just a date-based timestamp.
1. Using TIMESTAMP_CONVERT_FROM_DATE_TIME
(Recommended)
This method converts a date and time into a full timestamp.
DATA: lv_date TYPE sy-datum, lv_time TYPE sy-uzeit, lv_timestamp TYPE timestamp.
lv_date = sy-datum. " Current datelv_time = sy-uzeit. " Current time
CALL FUNCTION 'TIMESTAMP_CONVERT_FROM_DATE_TIME' EXPORTING i_date = lv_date i_time = lv_time IMPORTING e_timestamp = lv_timestamp.
WRITE: 'Timestamp:', lv_timestamp.
✅ Recommended for most use cases ✅ Uses both date & time for full timestamp format
2. Manually Construct Timestamp (If Time is Not Needed)
If you just need the timestamp representation of a date (without time), you can manually concatenate it:
DATA: lv_date TYPE sy-datum, lv_timestamp TYPE timestamp.
lv_date = sy-datum.lv_timestamp = lv_date && '000000'. " Append '000000' as time (midnight)
WRITE: 'Timestamp:', lv_timestamp.
✅ Simple method if time isn’t required ✅ Works well when using date-only values
3. Using CONVERT_DATE_TIME_TO_TIMESTAMP
This function module also converts a date and time into a timestamp.
DATA: lv_date TYPE sy-datum, lv_time TYPE sy-uzeit, lv_timestamp TYPE timestamp.
lv_date = sy-datum.lv_time = sy-uzeit.
CALL FUNCTION 'CONVERT_DATE_TIME_TO_TIMESTAMP' EXPORTING i_datlo = lv_date i_timlo = lv_time IMPORTING e_timestamp = lv_timestamp.
WRITE: 'Timestamp:', lv_timestamp.
✅ Similar to TIMESTAMP_CONVERT_FROM_DATE_TIME
✅ Useful when dealing with time zones
4. Using CL_ABAP_TSTMP
Class (Newer Approach)
If you’re working in newer ABAP versions, you can use CL_ABAP_TSTMP
:
DATA: lv_date TYPE d, lv_time TYPE t, lv_timestamp TYPE timestamp.
lv_date = sy-datum.lv_time = sy-uzeit.
lv_timestamp = cl_abap_tstmp=>create_timestamp( i_date = lv_date i_time = lv_time ).
WRITE: 'Timestamp:', lv_timestamp.
✅ Object-oriented approach ✅ Best suited for modern ABAP environments
Which Method to Use?
Scenario | Recommended Method |
---|---|
Convert Date & Time to Timestamp | TIMESTAMP_CONVERT_FROM_DATE_TIME |
Convert Only Date to Timestamp | Manually append '000000' |
Convert with Time Zone Handling | CONVERT_DATE_TIME_TO_TIMESTAMP |
Object-Oriented Approach | CL_ABAP_TSTMP=>CREATE_TIMESTAMP |