0
public static DateTime Final_Date_Provider(DateTime start, TimeSpan offset)
{
//code
}
此方法的梅索德應該caclcule結束日期= START +偏移
問題是:
我希望它能夠從早上8點到下午5點完成,並取消延遲12至12:30。
更新!
public static DateTime Final_Date_Provider(DateTime start, TimeSpan offset)
{
const int hoursPerDay = 8;
const int startHour = 8;
// Don't start counting hours until start time is during working hours
if (start.TimeOfDay.TotalHours > startHour + hoursPerDay)
start = start.Date.AddDays(1).AddHours(startHour);
if (start.TimeOfDay.TotalHours < startHour)
start = start.Date.AddHours(startHour);
if (start.DayOfWeek == DayOfWeek.Saturday)
start.AddDays(2);
else if (start.DayOfWeek == DayOfWeek.Sunday)
start.AddDays(1);
// Calculate how much working time already passed on the first day
TimeSpan firstDayOffset =
start.TimeOfDay.Subtract(TimeSpan.FromHours(startHour));
// Calculate number of whole days to add
int wholeDays = (int)(offset.Add(firstDayOffset).TotalHours/hoursPerDay);
// How many hours off the specified offset does this many whole days consume?
TimeSpan wholeDaysHours = TimeSpan.FromHours(wholeDays * hoursPerDay);
// Calculate the final time of day based on the number of whole days spanned and the specified offset
TimeSpan remainder = offset - wholeDaysHours;
// How far into the week is the starting date?
int weekOffset = ((int)(start.DayOfWeek + 7) - (int)DayOfWeek.Monday) % 7;
// How many weekends are spanned?
int weekends = (int)((wholeDays + weekOffset)/5);
// Calculate the final result using all the above calculated values
return start.AddDays(wholeDays + weekends * 2).Add(remainder);
}
你能後至今你已經嘗試的代碼? – Baldrick