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.
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
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
Hi Shiva,
Thanks a lot!
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
Thanks Horst for your valuable input.
Saurabh
Is there a way to handle Milliseconds?
Hello Vincent,
No, sorry.
Bye,
. Horst
Nice Blog Siva...