Skip to Content
Author's profile photo Former Member

Calculate days & minutes between two DateTime (LOCAL_DateTime) fields.

Hi Guys,

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

LocalDateTime.PNG

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

GlobalDateTime.PNG

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).

  BO.PNG

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

  QA Screen.PNG

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.


output.PNG

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

Regards,

Sambasiva G

Assigned tags

      7 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member
      Blog Post Author

      Hi,

      If we need to find the seconds difference also in the date time, then write below code.

      var GetSecondsOfTime1 = Numeric.ParseFromString(time1.GetSecond());

      var GetSecondsOfTime2 = Numeric.ParseFromString(time2.GetSecond());

      if (GetSecondsOfTime1 > GetSecondsOfTime2)

      {

          SecondsDifference =60- ( GetSecondsOfTime1 - GetSecondsOfTime2);

          TimeDifference = TimeDifference - 1;  //minus 1 minute

      }

      else

      {

          SecondsDifference = GetSecondsOfTime2 - GetSecondsOfTime1;

          if(SecondsDifference !=0)

         {

            TimeDifference = TimeDifference - 1;

         }

      }

      //For Hours... we'll convert total minutes to hours and minutes.. like below (not including hours in our logic)

      eg: var Realminutes= TimeDifference%60;

      var RealHours= (TimeDifference-Realminutes)/60;

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

      this.difference = TotalDifference;

      Regards,

      Shiva G

      Author's profile photo Saurabh Sharma
      Saurabh Sharma

      Hi Shiva,

      Thanks a lot!

      Author's profile photo Horst Schaude
      Horst Schaude

      Hello Saurabh,

      You would also be able to convert the LOCAL_DateTime to the GLOBAL_DateTime, use the comprehensive library for the calculations and convert it back.

      HTH,

          Horst

      Author's profile photo Saurabh Sharma
      Saurabh Sharma

      Thanks Horst for your valuable input.

      Saurabh

      Author's profile photo Vincent Vancalbergh
      Vincent Vancalbergh

      Is there a way to handle Milliseconds?

      Author's profile photo Horst Schaude
      Horst Schaude

      Hello Vincent,

      No, sorry.

      Bye,
      .    Horst

      Author's profile photo Tasnim Ara
      Tasnim Ara

      Nice Blog Siva...