How to Calculate the Difference Between Two DateTime Fields in SAP C4C
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();
SAP C4C Technical Consultant