Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

Hi Guys,

In SAP Cloud Application Studio there is no standard functions to find out the difference between two DateTime(LOCAL_DateTime ) fields.

If it is a GLOBAL_DateTime field we have some standard functions like below.

So, this document will explain you how to find out DateTime difference between two LOCAL_DateTime fields.

To achieve this i have written code from SDK.

Step 1: Create a Custom BO with three fields like below (for testing purpose i have created it).

 

Step 2: Create UI Screens and my QAF screen will look like below.

 

Step 3: In the script (AfterModify / BeforeSave) write the code like below.

    a. First get the values from your DateTime fields and store it to the variables.

           var val1 = this.DateTime1.content; // replace ur Date Time field here

            var val2 = this.DateTime2.content; // replace ur Date Time field here

   

   b. Convert DateTime value as a string value and replace all character values (we need only numeric values)

         

           var dateTimestring1 = val1.ToString().Replace("-", "").Replace(":", "").Replace("T", "").Replace("Z", "");

            var dateTimestring2 = val2.ToString().Replace("-", "").Replace(":", "").Replace("T", "").Replace("Z", "");


     c. Get Date and Time values from our string variables.

         

            var date1 = Date.ParseFromString(dateTimestring1.Substring(0, 8));

            var time1 = Time.ParseFromString(dateTimestring1.Substring(8));

            var date2 = Date.ParseFromString(dateTimestring2.Substring(0, 8));

            var time2 = Time.ParseFromString(dateTimestring2.Substring(8));


     d. Get the Hour and Minute of the Time variable.

         

            var GetHourOfTime1 = time1.GetHour();

            var GetMinuteOfTime1 = time1.GetMinute();

            var GetHourOfTime2 = time2.GetHour();

            var GetMinutesOfTime2 = time2.GetMinute();

      e. To differentiate two Date fields we have a standard function "Delta()", by using it find out the Dates difference.

         var DateDifference = date1.Delta(date2).ConvertToDays();


Now, the tricky thing is we need to find out Time Difference and it will effect on the dates difference as well.

--> If first field Hour is greater than second field (or) "Both the hours are same && first field Minute is greater than the second field Minute, follow the below logic.

     else calculate time difference by using Delta function.

    

    var TimeDifference;

     if ((GetHourOfTime1 > GetHourOfTime2) || (GetHourOfTime1 == GetHourOfTime2) && (GetMinutesOfTime1 > GetMinutesOfTime2))

     {

       TimeDifference = time2.Delta(time1).ConvertToMinutes();

        DateDifference = DateDifference - 1;

        TimeDifference = 1440 - TimeDifference;  // 24 hours (24*60=1440)

     }

    else

     {

        TimeDifference = time2.Delta(time1).ConvertToMinutes();

     }

Now we got the days and minutes between two different Date Time fields and assign to the difference field like below.

   var TotalDifference = DateDifference.ToString() + " days & " + TimeDifference.ToString() + " minutes";

   this.difference = TotalDifference;


Now the Output looks like below.


I hope it will be useful, if any one is trying to acheive this kind of development.

Regards,

Sambasiva G

7 Comments
Labels in this area