Zak*_*aki 5 c# datetime weekend
我正在尝试将日期添加到某个日期,但希望新日期不包括周末,而且只能在9.00到17.00之间.
例:
第一种情况:
if my date is 23/02/2012 16:00:00.
if I add 4 hours to that my new date should be 24/02/2012 12:00:00 (which will be within working hours)
Run Code Online (Sandbox Code Playgroud)
第二种情况:
if my date is 24/02/2012 09:00:00 (which is a friday)
if I add 24 hours then the new date should be 27/02/2012 09:00:00 (which would be monday next week at 9am)
Run Code Online (Sandbox Code Playgroud)
到目前为止我得到了这个,但我被卡住,因为这不会计入过去的任何日期说日期过去是2012年2月10日(上周五):
private static void ExcludeWeekend(Datetime dt)
{
DateTime todaysDate = DateTime.Today;
DateTime dueDate = null;
if (dueDate.DayOfWeek == DayOfWeek.Friday)
{
dueDate.AddHours(48);
}
else if (dueDate.DayOfWeek == DayOfWeek.Saturday)
{
dueDate.AddHours(72);
}
}
Run Code Online (Sandbox Code Playgroud)
static DateTime AddWithinWorkingHours(DateTime start, TimeSpan offset)
{
const int hoursPerDay = 8;
const int startHour = 9;
// 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);
}
Run Code Online (Sandbox Code Playgroud)
小智 7
您可以使用类CalendarDateAdd从时间周期图书馆.NET:
// ----------------------------------------------------------------------
public void CalendarDateAddSample()
{
CalendarDateAdd calendarDateAdd = new CalendarDateAdd();
// use only weekdays
calendarDateAdd.AddWorkingWeekDays();
// setup working hours
calendarDateAdd.WorkingHours.Add( new HourRange( new Time( 09 ), new Time( 17 ) ) );
DateTime start = new DateTime( 2012, 2, 23 ); // start date
TimeSpan offset = new TimeSpan( 4, 0, 0 ); // 4 hours
DateTime? end = calendarDateAdd.Add( start, offset ); // end date
Console.WriteLine( "start: {0}", start );
Console.WriteLine( "offset: {0}", offset );
Console.WriteLine( "end: {0}", end );
} // CalendarDateAddSample
Run Code Online (Sandbox Code Playgroud)