[英]finding end date from start date with removing a delay
I have a methode like this 我有这样的方法
public static DateTime Final_Date_Provider(DateTime start, TimeSpan offset)
{
//code
}
this method is supposed to caclcule EndDate = start + offset 该方法应该计算EndDate = start + offset
the problem is : 问题是 :
i want it to do that from 8 am to 5 pm with removing the delay 12 until 12:30 我希望它从上午8点到下午5点执行此操作,并删除延迟12到12:30
and sundays and holidays. 以及周日和节假日。
UPDATE ! 更新!
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);
}
Try something like: 尝试类似:
public static DateTime Final_Date_Provider(DateTime start, TimeSpan offset)
{
// If DateTime.Now is between 8 am and 5pm
// If DateTime.Now is not between 12 and 12:30
// DateTime changed = start.Add(offset);
// If Sunday || Holiday
// Do something different
return changed;
}
You can use the CalendarDateAdd class of the Time Period Library for .NET : 您可以使用.NET时间周期库的CalendarDateAdd类:
// ----------------------------------------------------------------------
public void FinalDateProvider()
{
DateTime now = new DateTime( 2014, 5, 16, 9, 0, 0 );
TimeSpan offset = new TimeSpan( 15, 0, 0 );
Console.WriteLine( "{0} + {1} = {2}", now, offset, CalcFinalDate( now, offset ) );
} // FinalDateProvider
// ----------------------------------------------------------------------
public DateTime? CalcFinalDate( DateTime start, TimeSpan offset )
{
CalendarDateAdd dateAdd = new CalendarDateAdd();
dateAdd.AddWorkingWeekDays();
dateAdd.WorkingHours.Add( new HourRange( 8, 12 ) );
dateAdd.WorkingHours.Add( new HourRange( new Time( 12, 30 ), new Time( 17 ) ) );
return dateAdd.Add( start, offset );
} // CalcFinalDate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.