SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
Development resources, articles, tutorials, code samples, tools and downloads for SAP HANA and ABAP, HANA Database, SQLScript, SAP UI5, Screen Personas, Web Dynpro, Workflow

Get Current Timestamp and Calculate Time Difference in ABAP


ABAP developers can calculate execution time by calculating time difference between two timestamp variables in their ABAP programs by code. In this ABAP tutorial, I want to share how to read current timestamp value into a local variable and use timestamp variables to calculate time difference between two events.

How to get current timestamp value in ABAP

If you develop code using ABAP for SAP, you frequently require to get current timestamp into ABAP variables. ABAP programmers can read current timestamp value into a variable using following ABAP code.

get time stamp field data(lv_timestamp).
Code

This code will define a new variable named lv_timestamp (for local variable timestamp) and set its value.
The timestamp value will include time data including years to time in seconds detail.


Calculate time difference in ABAP using timestamp variables

If you want to calculate time difference between two statement execution, following ABAP code can help programmers time passed during execution of a LOOP statement in sample SAP report for example.

* start time
get time stamp field data(lv_ts_start).

* sample task
loop at lt_data reference into data(lr_data).
 ...
endloop.

* end time
get time stamp field data(lv_ts_end).

* calculate time difference between timestamp values in ABAP
data(lv_timepassed_seconds) = cl_abap_tstmp=>subtract(
 tstmp1 = lv_ts_end
 tstmp2 = lv_ts_start ).
Code

ABAP code for timestamp calculations

Please note that I have used ABAP class cl_abap_tstmp (Arithmetic and Conversion for Time Stamps) and its subtract method to calculate the difference between two timestamp values.

ABAP timestamp variables in debug mode



SAP HANA and ABAP

Install SAP Free
CRM Companies List
Web Based CRM Software


Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.