Forum Discussion
QuickBaseCoachD
7 years agoQrew Captain
Here is a revised formula. There is probably a simpler formula given that you are not excluding weekends, but since this seems to work, it should do the trick. I did have to use 11:59 instead of 12:00 to get it to work, so I also changed the 4:00 back one minute so that the results did not have unexpected decimals.
// input your own business hours and datetime fields which are your start and end.
var timeofday DayStartTime = ToTimeOfDay("3:59 am");
var timeofday DayEndTime = ToTimeOfDay("11:59 pm");
var datetime StartClock = [Start Date Time];
var datetime EndClock = [End Date Time];
var DateTime StartDateTime = Max($StartClock, ToTimestamp(ToDate($StartClock), $DayStartTime));
var DateTime EndDateTimeTesting = If(IsNull($EndClock), Now() ,$EndClock);
var DateTime EndDateTime = Min($EndDateTimeTesting, ToTimestamp(ToDate($EndDateTimeTesting), $DayEndTime));
var Number DayDays = ToDays(ToDate($EndDateTime) - ToDate($StartDateTime) + Days(1)); //(we count each day as a full workday)
var number HoursBeforeStartEndAdjustment = $DayDays * ToHours($DayEndTime - $DayStartTime);
var datetime StartDateTimeBounded =
If(ToTimeOfDay($StartDateTime) < $DayStartTime, ToTimestamp(ToDate($StartDateTime), $DayStartTime), $StartDateTime);
var datetime EndDateTimeBounded =
If(ToTimeOfDay($EndDateTime) > $DayEndTime, ToTimestamp(ToDate($EndDateTime), $DayEndTime), $EndDateTime);
Max(0,
Round($HoursBeforeStartEndAdjustment
- Max(0,ToHours(ToTimeOfDay($StartDateTimeBounded) - $DayStartTime))
- Max(0,ToHours($DayEndTime - ToTimeOfDay($EndDateTimeBounded))),0.1))
// input your own business hours and datetime fields which are your start and end.
var timeofday DayStartTime = ToTimeOfDay("3:59 am");
var timeofday DayEndTime = ToTimeOfDay("11:59 pm");
var datetime StartClock = [Start Date Time];
var datetime EndClock = [End Date Time];
var DateTime StartDateTime = Max($StartClock, ToTimestamp(ToDate($StartClock), $DayStartTime));
var DateTime EndDateTimeTesting = If(IsNull($EndClock), Now() ,$EndClock);
var DateTime EndDateTime = Min($EndDateTimeTesting, ToTimestamp(ToDate($EndDateTimeTesting), $DayEndTime));
var Number DayDays = ToDays(ToDate($EndDateTime) - ToDate($StartDateTime) + Days(1)); //(we count each day as a full workday)
var number HoursBeforeStartEndAdjustment = $DayDays * ToHours($DayEndTime - $DayStartTime);
var datetime StartDateTimeBounded =
If(ToTimeOfDay($StartDateTime) < $DayStartTime, ToTimestamp(ToDate($StartDateTime), $DayStartTime), $StartDateTime);
var datetime EndDateTimeBounded =
If(ToTimeOfDay($EndDateTime) > $DayEndTime, ToTimestamp(ToDate($EndDateTime), $DayEndTime), $EndDateTime);
Max(0,
Round($HoursBeforeStartEndAdjustment
- Max(0,ToHours(ToTimeOfDay($StartDateTimeBounded) - $DayStartTime))
- Max(0,ToHours($DayEndTime - ToTimeOfDay($EndDateTimeBounded))),0.1))
EndyLu
6 years agoQrew Trainee
how will this formula works if I want to exclude weekends? I only want to include regular business hours 9-5 Monday to Friday!
------------------------------
Endy Lu
------------------------------
------------------------------
Endy Lu
------------------------------
- AustinK6 years agoQrew CommanderGo with one of the original formulas above. If you read through the thread there is a point where they got the formula to work perfectly for their needs except it was still excluding weekends. I'd imagine if you took that formula and changed the start and end times to be 9 am and 5 pm that it would work for you or be really close to working for you and only need some small adjustments.