maddyyikes
IS-IT--Management
Hi,
I obtained the following code snippet from this site used for calculating business hours between the opening and closing of a ticket. The code seems to accurately count business hours for all tickets opened during the business hours. However, the code gives me incorrect values for any ticket opened before 8 AM or after 5 PM (business hours). For instance, if the open time is 7:00:00 PM (after business hours) and the close time is 3:00 PM next day then it should reflect 7 business hours as the total duration for the handling of the ticket (next day 8 AM to 3 PM = 7 business hours). However it gives me unusual values such as 1 day 13 hours which is not correct. Please let me know as to how I should modify the code given below to incorporate business hour calculation for tickets opened after business hours. This is extremely urgent and any help in this regard will be greatly appreciated. Thanks in advance. The code snippet is given below as follows:
local numbervar numDayStartHour:=8; //The hour that work starts in 24 hour time.
local numbervar numDayStartMinute:=0; //The minutes value of the time when work starts
local numbervar numDayEndHour:=17; //The hour that work ends in 24 hour time
local numbervar numDayEndMinute:=0; //The minutes value of the time when work ends
local numbervar numWholeDaysBetween; //The whole days between two datetimes
local timevar tmDayStart:=time(numDayStartHour,numDayStartMinute,0); //Convert the start hour and minutes to a time
local timevar tmDayEnd:=time(numDayEndHour,numDayEndMinute,0); //Convert the end hour and minutes to a time
local numbervar numSecondsInWholeDay; //Minutes in a whole day based on tmDayStart and tmDayEnd
local numbervar numSecondsInStartDay;
local numbervar numSecondsInEndDay;
local numbervar numTotalSeconds;
local numbervar numDivider:=60; //This is here in case we want to switch our formula over to seconds, later.
//whole days between, excluding weekends
// this is a pretty standard method
numWholeDaysBetween:=datediff("d",{probsummarym1.open.time},{probsummarym1.close.time})
-
(DateDiff ("ww", {probsummarym1.open.time}, {probsummarym1.close.time}, crSunday) +
DateDiff ("ww", {probsummarym1.open.time}, {probsummarym1.close.time}, crSaturday));
//Calculate seconds in a whole day
numSecondsInWholeDay:=(tmDayEnd-tmDayStart);
//Calculate seconds in start day
// beginning time to tmDayEnd
numSecondsInStartDay:=
(
(tmDayEnd-(maximum((tmDayStart) to (time({probsummarym1.open.time}))))) //The maximum is used to determine if we're inside the workday
);
//Calculate seconds in end day
// tmDayStart to End Time
numSecondsInEndDay:=
(
((minimum((time({probsummarym1.close.time})) to tmDayEnd))-tmDayStart) //The minimum is used to determine if we're inside the workday
);
//Calculate all seconds:
numTotalSeconds:=
numSecondsInEndDay+numSecondsInStartDay //First and Last day minutes
+
(((numWholeDaysBetween)-1)*numSecondsInWholeDay); //Whole days between minutes. The -1 is there because of the first and last day being included above.
numbervar tsecs1 := numTotalSeconds;
numbervar ndays1 := truncate(tsecs1/86400);
tsecs1 := remainder(tsecs1,86400);
numbervar nhours1 := truncate(tsecs1/3600);
tsecs1 := remainder(tsecs1,3600);
numbervar nmin1 := truncate(tsecs1/60);
tsecs1 := remainder(tsecs1,60);
totext(ndays1,0)+" days "+totext(nhours1,0)+" hours " + totext(nmin1,0) + "minutes"
I obtained the following code snippet from this site used for calculating business hours between the opening and closing of a ticket. The code seems to accurately count business hours for all tickets opened during the business hours. However, the code gives me incorrect values for any ticket opened before 8 AM or after 5 PM (business hours). For instance, if the open time is 7:00:00 PM (after business hours) and the close time is 3:00 PM next day then it should reflect 7 business hours as the total duration for the handling of the ticket (next day 8 AM to 3 PM = 7 business hours). However it gives me unusual values such as 1 day 13 hours which is not correct. Please let me know as to how I should modify the code given below to incorporate business hour calculation for tickets opened after business hours. This is extremely urgent and any help in this regard will be greatly appreciated. Thanks in advance. The code snippet is given below as follows:
local numbervar numDayStartHour:=8; //The hour that work starts in 24 hour time.
local numbervar numDayStartMinute:=0; //The minutes value of the time when work starts
local numbervar numDayEndHour:=17; //The hour that work ends in 24 hour time
local numbervar numDayEndMinute:=0; //The minutes value of the time when work ends
local numbervar numWholeDaysBetween; //The whole days between two datetimes
local timevar tmDayStart:=time(numDayStartHour,numDayStartMinute,0); //Convert the start hour and minutes to a time
local timevar tmDayEnd:=time(numDayEndHour,numDayEndMinute,0); //Convert the end hour and minutes to a time
local numbervar numSecondsInWholeDay; //Minutes in a whole day based on tmDayStart and tmDayEnd
local numbervar numSecondsInStartDay;
local numbervar numSecondsInEndDay;
local numbervar numTotalSeconds;
local numbervar numDivider:=60; //This is here in case we want to switch our formula over to seconds, later.
//whole days between, excluding weekends
// this is a pretty standard method
numWholeDaysBetween:=datediff("d",{probsummarym1.open.time},{probsummarym1.close.time})
-
(DateDiff ("ww", {probsummarym1.open.time}, {probsummarym1.close.time}, crSunday) +
DateDiff ("ww", {probsummarym1.open.time}, {probsummarym1.close.time}, crSaturday));
//Calculate seconds in a whole day
numSecondsInWholeDay:=(tmDayEnd-tmDayStart);
//Calculate seconds in start day
// beginning time to tmDayEnd
numSecondsInStartDay:=
(
(tmDayEnd-(maximum((tmDayStart) to (time({probsummarym1.open.time}))))) //The maximum is used to determine if we're inside the workday
);
//Calculate seconds in end day
// tmDayStart to End Time
numSecondsInEndDay:=
(
((minimum((time({probsummarym1.close.time})) to tmDayEnd))-tmDayStart) //The minimum is used to determine if we're inside the workday
);
//Calculate all seconds:
numTotalSeconds:=
numSecondsInEndDay+numSecondsInStartDay //First and Last day minutes
+
(((numWholeDaysBetween)-1)*numSecondsInWholeDay); //Whole days between minutes. The -1 is there because of the first and last day being included above.
numbervar tsecs1 := numTotalSeconds;
numbervar ndays1 := truncate(tsecs1/86400);
tsecs1 := remainder(tsecs1,86400);
numbervar nhours1 := truncate(tsecs1/3600);
tsecs1 := remainder(tsecs1,3600);
numbervar nmin1 := truncate(tsecs1/60);
tsecs1 := remainder(tsecs1,60);
totext(ndays1,0)+" days "+totext(nhours1,0)+" hours " + totext(nmin1,0) + "minutes"