I\'m a SQL guy, but I need a function to calculate the number of weekdays between two dates in VB.NET. I don\'t need to worry about holidays. My attempts unfortunately have
This may help someone else looking for this. This function doesn't loop day by day.
public static double WorkDays(DateTime start, DateTime end)
{
var delta = end.AddDays(1) - start;
var fullWeeks = (int)(delta.TotalDays / 7);
var workDays = fullWeeks * 5;
var partialWeekDays = delta.TotalDays % 7;
if (partialWeekDays > 0)
{
var startWeekday = start.DayOfWeek;
var endWeekday = end.DayOfWeek;
if (startWeekday == DayOfWeek.Sunday || startWeekday == DayOfWeek.Saturday)
partialWeekDays--;
if (startWeekday > DayOfWeek.Sunday && startWeekday < DayOfWeek.Saturday &&
startWeekday > endWeekday)
partialWeekDays--;
if ((endWeekday == DayOfWeek.Sunday || endWeekday == DayOfWeek.Saturday) && endWeekday != startWeekday)
partialWeekDays--;
if (endWeekday > DayOfWeek.Sunday && endWeekday < DayOfWeek.Saturday &&
startWeekday > endWeekday)
partialWeekDays--;
}
return workDays + partialWeekDays;
}