添加小时到日期时间但不包括周末,应该在工作时间之间

时间:2012-02-23 13:26:42

标签: 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)

第二种情况:

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)

到目前为止,我得到了这个,但我被困住了,因为这不会计入过去的任何日期说过去的日期是10/02/2012(上周五):

   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);
                }
    }

4 个答案:

答案 0 :(得分:7)

 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);
 }

答案 1 :(得分:6)

您可以使用Time Period Library for .NET中的 CalendarDateAdd 类:

// ----------------------------------------------------------------------
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

答案 2 :(得分:2)

    var d1 = DateTime.Now;
    var ts = TimeSpan.FromHours(40);
    var d2 = d1 + ts;
    if(d2.DayOfWeek == DayOfWeek.Saturday) {
        d2 = d2.AddDays(2);
    }else if(d2.DayOfWeek == DayOfWeek.Sunday){
        d2 = d2.AddDays(1);
    }

如果你真的想把它作为一个扩展名:

var d2 = DateTime.Now.AddSkipWeekend(TimeSpan.FromHours(40));

static class DateExtensions { 
    public static DateTime AddSkipWeekend(this DateTime date1, TimeSpan ts){
        DateTime d2 = date1 + ts;
        if(d2.DayOfWeek == DayOfWeek.Saturday) {
            d2 = d2.AddDays(2);
        } else if(d2.DayOfWeek == DayOfWeek.Sunday) {
            d2 = d2.AddDays(1);
        }
        return d2;
    }
}

修改:刚刚意识到您的非工作时间要求:

var d2 = DateTime.Now.AddSkipWeekend(TimeSpan.FromHours(50),TimeSpan.FromHours(9),TimeSpan.FromHours(17));

public static DateTime AddSkipWeekend(this DateTime date1, TimeSpan addTime, TimeSpan workStart, TimeSpan workEnd)
{
    DateTime d2 = date1 + addTime;
    if(d2.TimeOfDay < workStart) {
        d2 = d2.Add(workStart - d2.TimeOfDay);
    } else if(d2.TimeOfDay > workEnd) {
        d2 = d2.Add(TimeSpan.FromHours(12) - d2.TimeOfDay);
    }
    if(d2.DayOfWeek == DayOfWeek.Saturday) {
        d2 = d2.AddDays(2);
    } else if(d2.DayOfWeek == DayOfWeek.Sunday) {
        d2 = d2.AddDays(1);
    }
    return d2;
}

答案 3 :(得分:1)

试试这个:

public static DateTime Add(DateTime dt, TimeSpan t)
{
    while (true)
    {
        dt = Max(dt, dt.Date.AddHours(9));
        DateTime x = Min(dt + t, dt.Date.AddHours(17));
        // Console.WriteLine("{0} -> {1} ({2})", dt, x, x - dt);
        t -= x - dt;
        dt = x;
        if (t == TimeSpan.Zero) { return dt; }
        do { dt = dt.Date.AddDays(1); } while (dt.IsWeekendDay());
    }
}

来自here的帮助方法。

示例1:

var result = Add(DateTime.Parse("23/02/2012 16:00:00"), TimeSpan.FromHours(4));
// result == {24/02/2012 12:00:00}

23/02/2012 16:00:00 -> 23/02/2012 17:00:00 (01:00:00)
24/02/2012 09:00:00 -> 24/02/2012 12:00:00 (03:00:00)

示例2:

var result = Add(DateTime.Parse("24/02/2012 09:00:00"), TimeSpan.FromHours(24));
// result == {28/02/2012 17:00:00}

24/02/2012 09:00:00 -> 24/02/2012 17:00:00 (08:00:00)
27/02/2012 09:00:00 -> 27/02/2012 17:00:00 (08:00:00)
28/02/2012 09:00:00 -> 28/02/2012 17:00:00 (08:00:00)