Skip to main content

Posts

Showing posts from October, 2011

Business Days in c# (a very fast way)

Hi, Today I needed a simple code that returns the business days between two dates and ended with this: public static int GetFullWorkingDaysBetween(DateTime firstDate, DateTime lastDate, IEnumerable holidays) { if (firstDate > lastDate)// Swap the dates if firstDate > lastDate { DateTime tempDate = firstDate; firstDate = lastDate; lastDate = tempDate; } int days = (int)(lastDate.Subtract(firstDate).Ticks / TimeSpan.TicksPerDay); int weekReminder = days % 7; if (weekReminder > 0) { switch (firstDate.DayOfWeek) { case DayOfWeek.Monday: days = days - ((weekReminder > 5) ? 1 : 0); // Another way for this: //days = days - ((int)weekReminder % 5); // but i think its more expensive