Forum Discussion
XavierFan
10 years agoQrew Cadet
Try this for the formula - for the Formula - Numeric field:
var DateTime StartTime = If([Task Status]="Completed", [Time Completed], Now() );
var Number HoursIncludingWeekends = ToHours( [Deadline] - $StartTime );
var Number NumWeekendDays = ToDays( ToDate([Deadline]) - ToDate($StartTime) ) - WeekdaySub ( ToDate([Deadline]), ToDate($StartTime) );
$HoursIncludingWeekends - ($NumWeekendDays * 24)
The first line calculates StartTime - which would be [Time Completed] if [Task Status] = "Completed", else it's the current time (Now() ). This saves having to repeat the calculation again further down.
HoursIncludingWeekends is calculated next - as the total number of hours between [Deadline] and StartTime.
NumWeekendDays calculates the number of weekend days between [Deadline] and StartTime. It does this by converting all the Date/Times to Dates (with the ToDate() function), then figuring out how many days there are between them, and finding the difference when you exclude weekend days using the WeekdaySub() function.
Finally - we give the final result which is simply the total number of hours (HoursIncludingWeekends) and subtracting the number of weekend hours (which would be NumWeekendDays * 24).
Hope this helps!
var DateTime StartTime = If([Task Status]="Completed", [Time Completed], Now() );
var Number HoursIncludingWeekends = ToHours( [Deadline] - $StartTime );
var Number NumWeekendDays = ToDays( ToDate([Deadline]) - ToDate($StartTime) ) - WeekdaySub ( ToDate([Deadline]), ToDate($StartTime) );
$HoursIncludingWeekends - ($NumWeekendDays * 24)
The first line calculates StartTime - which would be [Time Completed] if [Task Status] = "Completed", else it's the current time (Now() ). This saves having to repeat the calculation again further down.
HoursIncludingWeekends is calculated next - as the total number of hours between [Deadline] and StartTime.
NumWeekendDays calculates the number of weekend days between [Deadline] and StartTime. It does this by converting all the Date/Times to Dates (with the ToDate() function), then figuring out how many days there are between them, and finding the difference when you exclude weekend days using the WeekdaySub() function.
Finally - we give the final result which is simply the total number of hours (HoursIncludingWeekends) and subtracting the number of weekend hours (which would be NumWeekendDays * 24).
Hope this helps!