.NET Date Compare: Count the amount of working days since a date?

This’ll do what you want it to. It should be easy enough to convert to VB.NET, it’s been too long for me to be able to do it though.

DateTime start = DateTime.Now;
DateTime end = start.AddDays(9);
IEnumerable<DateTime> holidays = new DateTime[0];

// basic data
int days = (int)(end - start).TotalDays;
int weeks = days / 7;

// check for a weekend in a partial week from start.
if (7- (days % 7) <= (int)start.DayOfWeek)
    days--;
if (7- (days % 7) <= (int)start.DayOfWeek)
    days--;

// lose the weekends
days -= weeks * 2;

foreach (DateTime dt in holidays)
{
    if (dt > start && dt < end)
        days--;
}

Leave a Comment