3

How to Calculate the Difference Between Two DateTime Fields in SAP C4C

 1 year ago
source link: https://blogs.sap.com/2023/04/05/how-to-calculate-the-difference-between-two-datetime-fields-in-sap-c4c/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
April 5, 2023 1 minute read

How to Calculate the Difference Between Two DateTime Fields in SAP C4C

Hello everyone,

In the course of implementing a business requirement, I recently had to calculate the difference between two DateTime fields in SAP C4C for reporting which calculates ticket assignment time from the creation. However, I couldn’t find any straightforward blog that provided a simple solution to this problem.

Fortunately, SAP provides a function called Delta() that can be used to calculate the difference between two DateTime fields. The only catch is that Delta() is only accessible for the GlobalDateTime data type.

So, how can you use Delta() for DateTime fields? It’s simple – just use the ConvertToGlobalDateTime() function after your DateTime field. This will convert your DateTime field to GlobalDateTime format, allowing you to use Delta().

The result of Delta() is returned in a “Duration” format, which was not convenient for my purposes. To convert the result into minutes or hours, I simply used the ConvertToMinutes() / ConvertToHour() functions, respectively.

Here’s a code snippet that demonstrates how to use Delta() to calculate the difference between two DateTime fields:

// Creation time in Datetime format.
var ZV_CreationDateTime = this.RequestInitialReceiptTimePoint.TimePoint.DateTime; 


//Converting local datetime to global datetime to use standard Delta function.
var ZV_CreationDateTimeGlobal = Library::DateTime.ConvertToGlobalDateTime(ZV_CreationDateTime);


//Ticket reassigned datetime.
var ZV_AssignedDateTime = Context.GetCurrentGlobalDateTime(); 


//Calculating the difference between two dates.
var ZV_DeltaDifference = ZV_AssignedDateTime.Delta(ZV_CreationDateTimeGlobal).ConvertToMinutes();
I hope this solution helps you as much as it helped me. If you have any feedback or questions, please don’t hesitate to leave a comment.

Best regards,

Pratik Shekokar

SAP C4C Technical Consultant


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK