在C#中,我如何计算两个日期之间的工作日差异?

时间:2013-11-28 02:57:23

标签: c# datetime timespan

我知道我可以用这个来计算两个日期之间的天差:

private int DaysFromStart(DateTime date, DateTime startDate)
{
    var days =  date.Subtract(startDate);
    return (int)days.TotalDays;
}

但现在我希望通过仅包括工作日来获得两个日期之间的差异。因此,例如,如果开始日期是星期五,要检查的日期是下一个星期一,则返回值将为1.

3 个答案:

答案 0 :(得分:6)

请考虑以下代码段...

int allDays = (int)date.Subtract(startDate).TotalDays;
int weekDays = Enumerable
                .Range(1, allDays)
                .Select(day => startDate.AddDays(day))
                .Count(day => day.DayOfWeek != DayOfWeek.Saturday && day.DayOfWeek != DayOfWeek.Sunday);

祝你好运!

答案 1 :(得分:0)

这将为您提供剩余的剩余天数(星期日和星期六除外)。变量startend代表开始日期和结束日期:

int DaysLeft = 0;
for (int i = 1;i <= (int)end.Subtract(start).TotalDays;i++)
{
    DayOfWeek day = start.AddDays(i).DayOfWeek;
    DaysLeft = (day == DayOfWeek.Sunday) || (day == DayOfWeek.Saturday)?DaysLeft:DaysLeft + 1;
}

答案 2 :(得分:0)

我使用扩展方法返回时间跨度中的工作日数量,甚至是两个日期之间的完整日期列表,然后您可以针对IEnumerable运行lambda表达式以获得所需内容。这是静态类的一个非常基本的例子:

public static class TimeSpanextensions
{
    public static IEnumerable<DateTime> GetDays(this DateTime start, DateTime end, bool returnWeekDays = false)
    {
        List<DateTime> daysInSpan = new List<DateTime>();
        //change following line to accomodate date format if needed
        TimeSpan ts = start.Date - end.Date;

        if(start < end)
        {
            DateTime _this = start;

            while (_this < end) 
            {
                _this = start.AddDays(1);
                daysInSpan.Add(_this);
            }
        }

        if(start > end)
        {
            DateTime _this = start;
            while(_this > end)
            {
                _this = _this.AddDays(-1);
                daysInSpan.Add(_this);
            }
        }

        if(returnWeekDays)
        {
            return daysInSpan.Where(d => d.DayOfWeek != DayOfWeek.Saturday && d.DayOfWeek != DayOfWeek.Sunday);
        }

        return daysInSpan;
    }

我使用以下示例:

public static IEnumerable<DateTime> WeekDays(DateTime one, DateTime two)
    {
        TimeSpan test = one - two;

        return one.GetDays(two, true).ToList();
    }

这是将时间跨度的天或工作日作为实际工作类型返回的基本示例,而不是TimeSpan返回的int类型的TimeSpan天。因此,如果您有两个日期StartDate和EndDate,那么您只需执行以下操作即可获得TimeSpane中存在的工作日。

   int count = Start < EndDate ? 1 : -1;
   DateTime newDate = StartDate;
   List<DateTime> allDaysList = new List<DateTime();

   while(newDate != EndDate)
   {

       newDate = newDate.AddDays(count); //will eithr add or subtract a day
       allDays.Add(newDate);           
   }


   //now return either only weekdays days
   allDays = allDays.Where(d => d.DayOfWeek != DayOfWeek.Saturday && d.DayOfWeek != DayOfWeek.Sunday);

如果您只想在TimeSpan中计算工作日,请执行以下操作:

 int workDays =  allDays.Where(d => d.DayOfWeek != DayOfWeek.Saturday && d.DayOfWeek != DayOfWeek.Sunday).Count();

这是解决此问题的不同方法。要扩展它,您只需创建一个静态方法,将其作为参数除外。例如:

List<DateTime> weekDaysOnly = StartsDate.GetDays(endDate).ToList();
//then once again do your business day lambda expression here to leave your list with weekdays only. Hope this helps.