# How to calculate the "business time" of an incident

Version 10

### Verified Product Versions

LANDESK Service Desk 7.7.xLANDESK Service Desk 7.8.xLANDESK Service Desk 2016.x

##### What is Business Time?

The term Business Time means the time between two dates/times taking into consideration the business hours as specified on a calendar within Service Desk.

##### How can I obtain this within a Calculation?

It is very straight forward obtaining a time span between two dates.  For example a calculation on the Incident object could read:

TimePeriod = DateTime.UtcNow - Incident.CreationDate

return String.Format("{0} Hour(s), {1} Minute(s), {2} Second(s)", Math.Floor(TimePeriod.TotalHours), TimePeriod.Minutes, TimePeriod.Seconds)

This method can be useful for occasions where you'd like to see how long an incident was open for (as in the example) or if an incident is ready to be closed automatically, etc.  However the time span you obtain will include nights and weekends which is not very useful when reporting against SLAs.

Note: The Math.Floor() method is used around the TimePeriod.TotalHours value because this will be a decimal.  For instance a time period of 1 hour and 45 minutes will be represented as 1.75 and the method rounds this down to 1.

Note:  See also the attachment "Using the business time function in a calculation.pdf" for a step by step example of creating this calculation.

##### The GetBusinessTime calculation function

From Service Desk 7.3.1 onwards there is a calculation function available to obtain the same TimeSpan as the example above does but use the business hours of the calendar on the Incident, Problem, etc. you're working on.  This is currently (as of Service Desk 7.3.1) undocumented and not visible in the list of functions within the Calculation Editor however is fully functional.  The syntax for the function is:

ProcessObject.GetBusinessTime(DateTimeFrom, DateTimeTo)

Using the new function the above example becomes:

TimePeriod = Incident.GetBusinessTime(Incident.CreationDate, DateTime.UtcNow)
return String.Format("{0} Hour(s), {1} Minute(s), {2} Second(s)", Math.Floor(TimePeriod.TotalHours), TimePeriod.Minutes, TimePeriod.Seconds)

The function assumes its being run from the process object (the Incident, Problem, etc.) and does this because it needs to pick up the Response Level of the object automatically.  This means that if you want to use the function on another object, for instance Resolution, you need to reference the relationship back to the process object before the function name.  For example to calculate the time an incident was open for when you're resolving it:

TimePeriod = Resolution.Incident.GetBusinessTime(Resolution.Incident.CreationDate, Resolution.CreationDate)
return String.Format("{0} Hour(s), {1} Minute(s), {2} Second(s)", Math.Floor(TimePeriod.TotalHours), TimePeriod.Minutes, TimePeriod.Seconds)

The result of the function is what's called a TimeSpan which can then be used in mathematical comparisons or formatted to a string.  For more information on TimeSpans and their use see section "Finding the time between two DateTime attributes (TimeSpans)" in the document: Using dates, times and time spans in Calculations

Important Note: When creating a BeforeSave calculation that includes the GetBusinessTime() function take extra care that the calculation dependencies are being added to the Dependencies panel on the right hand side of the calculation editor.  They may not get picked up automatically when used within the syntax of the function.

##### Time the clock was stopped

Unfortunately due to the way stopping and starting the clock works within Service Desk it is not possible to retrospectively look back and see when an incident had its clock stopped.  This means any use of the GetBusinessTime function will not take this time into consideration and return the same TimeSpan value whether the clock had been stopped during that time or not.  There is a Crystal report available which does take into account clock stoppage time which can be downloaded from this article: Time to Resolution report (replacement for Incident Duration By Days report)