Determine when a task will be completed
I would add some checks to make sure the date is correct. For instance your start date is on a Saturday.  

-- Updated from comment that task doesn't need to start during working times.

if (startTime.TotalDays &gt; 1)
{
    throw new ArgumentOutOfRangeException("startTime", "Start time greater than 24 hours");
}
if (endTime.TotalDays &gt; 1)
{
    throw new ArgumentOutOfRangeException("endTime", "End time greater than 24 hours");
}
if (endTime &lt; startTime)
{
    throw new ArgumentOutOfRangeException("endTime", "End time before start time");
}


For the logic you don't need to do a loop.

var numberOfDaystoStart = 0;
if (startOfTask.DayOfWeek == DayOfWeek.Saturday)
{
    numberOfDaystoStart = 2;
}
else if (startOfTask.DayOfWeek == DayOfWeek.Sunday)
{
    numberOfDaystoStart = 1;
}
else if (startOfTask.TimeOfDay &gt; endTime)
{
    // Start time after the end of the work day need to move to the next day
    numberOfDaystoStart = 1;
}

var startOffset = TimeSpan.Zero;
if (numberOfDaystoStart &gt; 0 || startOfTask.TimeOfDay &lt; startTime)
{
    // IF started on weekend or after work hours adjust for first work day and start of working hours
    var startOfTaskDate = startOfTask.AddDays(numberOfDaystoStart);
    startOffset =
        new DateTime(startOfTaskDate.Year, startOfTaskDate.Month, startOfTaskDate.Day,
            startTime.Hours, startTime.Minutes, startTime.Seconds, startTime.Milliseconds) - startOfTask;
}


var workingtime = endTime.Subtract(startTime);
var nonWorkingTime = TimeSpan.FromDays(1) - workingtime;

// This will give long from C# 
var numbersOfDays = durationOftask.Ticks/workingtime.Ticks;

// add in the duration then add in the extra time for non work hours
// add in the weekends
var endOfTask = startOfTask
    .Add(durationOftask)
    .Add(startOffset)
    .Add(new TimeSpan(numbersOfDays*nonWorkingTime.Ticks))
    // Add in the extra non-working time for each day
    .AddDays((numbersOfDays/5)*2); // Add in 2 extra days for every 5 days of work

// Need to account for starting later in the week and rolling over weekend
var extraDays = numbersOfDays%5;

var endOffset = TimeSpan.Zero;
if ((int) startOfTask.Add(startOffset).DayOfWeek + extraDays &gt; 5)
{
    endOffset = TimeSpan.FromDays(2);
}
// If we ended after the end of the day need to account for non-worktime
if (endOfTask.TimeOfDay &gt; endTime)
{
    endOffset = endOffset.Add(nonWorkingTime);
}

return endOfTask.Add(endOffset);


If starting before the start time or starting on the weekend if valid then there would need to be special code just for those cases but it would be at the top to just to add to the duration the extra days to move on to the start date.