Answered

Calculate the time difference between two different hours in a day

Hello, is there a similar function in the above function CHelper.getEffectiveDuration (Me, start date, end date) which is for calculating the difference between two dates. Is there a difference of two different hours in a day? If not, is there another way to code? Explain the reason for asking this question with a famous example, the application process. For example, in this process, when the user is on a daily leave, we can use the above function to display the difference between the start and end dates at the same time to the applicant in the requested number of days field. But if the leave is of the hour type, how can we show the requested hour amount to the applicant at the same time ??????????? I am waiting for your response . Thanks

Comments (4)

photo
2

Hi Iman,

Here is an example that will work for you:

var dDate1 = new DateTime(2021,09,21,15,0,0);

var dDate2 = new DateTime(2021,09,25,16,10,0);

var iMinutes = (dDate2-dDate1).TotalMinutes;

var iHours = (dDate2-dDate1).TotalHours;

CHelper.ThrowValidationAlert("Difference in minutes: "+ iMinutes +", OR in hours: "+ iHours);

The above code calculates the minutes/hours between two dates, including non-working days.

photo
1

Hello. The method you introduced is so great that it can calculate the total hours and total minutes of the time interval, but the problem is that in your example two consistency values ​​with a specific format are used. But I need to calculate only the number of hours requested from the two fields with the date and time format that the user receives as a difference variable. And the second point is that hourly leave in basically any organization is in the range of hours in a working day. Therefore, the conditional code must be added so that if the start and end dates of the leave are the same, our desired code will be executed and if the condition is not correct, it will show a message to the user. And the most important thing is that the sample made in your example must have a specific format. So I tried to modify the field input value with a special Bizaji function in your example format, but again I got a type mismatch error. I modified your code as follows, but I still have no results:

if ( <StartDate> = <EndDate>) //The time field of leave details and end of leave in the form of date and time is received from the user form. This code is executed in the change event at the same time.

{

var StartTime = CHelper.FormatDate(<StartDate>, "yyyy,M,d,hh,mm,ss");

var EndTime = CHelper.FormatDate(<EndDate>, "yyyy,M,d,hh,mm,ss");

var dDate1 = new DateTime(StartTime);

var dDate2 = new DateTime(EndTime);

var iHours = (dDate2-dDate1).TotalHours;

<TimeRequest> = iHours.ToString() + " Hours " ;

}

else

{

<TimeRequest> = "The requested hour must be within one day, otherwise it will not be displayed.";

}

In any case, thank you for your help. But if you guide me as I need to, I can complete my process. I look forward to your next tip. Thank you.

photo
2

Hi,

Could you please share with me the data type for those 2 inputs (StartDate and EndDate) and from where are you getting their values? Is it from the Form or from a web service? If those are of type Date then you don't need to do those transformations/formating. I used new DateTime to construct a date from scratch in order to use it in the above example.

So, your code should be:

var dStartDate = <dStartDate>; // this is Date type

var dEndDate = <dEndDate>; // this is Date type

<sTimeRequest> = (dEndDate-dStartDate).TotalHours + " Hours "; // this is String type

Also when you use Date fields in Bizagi forms make sure to enable the selection of time (it's called Show time on Format tab). If not, the user only selects the date and in the database, this will be saved as a date with 00h 00m 00s.

https://help.bizagi.com/bpm-suite/en/index.html?date.htm

If the StartDate is greater than EndDate the (dEndDate-dStartDate).TotalHours returns a negative number. You can use this in your if condition.

var dStartDate = <dStartDate>; // this is Date type

var dEndDate = <dEndDate>; // this is Date type

if ((dEndDate-dStartDate).TotalHours > 0) {

<sTimeRequest> = (dEndDate-dStartDate).TotalHours + " Hours "; // this is String type

} else {

<sTimeRequest> = "The requested hour must be within one day, otherwise it will not be displayed.";

}

There are so many solutions and all of them depend on what you would like to achieve. Let me know the business requirements and after that, I could recommend you the best approach.

photo
photo
1

Thank you Mr. Valeriu Bosneaga