Business days in .net
- dd4nyc
- Posts: 0
- Joined: Thu Jan 01, 2004 12:00 am
Business days in .net
Hi, is there a method in C# that calculates the number of business days between given dates, like networkdays in Excel? Thank you!
- tristanreid
- Posts: 0
- Joined: Thu Jan 01, 2004 12:00 am
Business days in .net
public static int networkdays(DateTime a, DateTime b, List holidays)
{
int nwdays = networkdays(a, b);
int aYear = a.Year;
int bYear = b.Year;
foreach (DateTime holiday in holidays)
{
if (a.CompareTo(holiday) 0) nwdays--;
}
return nwdays;
}
public static int networkdays(DateTime a, DateTime b)
{
int totalDays = ((TimeSpan)b.Subtract(a)).Days;
TimeSpan ts = b.Subtract(a);
int aBuffer = (int)DayOfWeek.Friday - (int)a.DayOfWeek;
int bBuffer = (int)DayOfWeek.Friday - (int)b.DayOfWeek;
int nwdays = (totalDays - aBuffer + bBuffer) * 5 / 7;
if (a.DayOfWeek == DayOfWeek.Sunday) aBuffer--;
if (a.DayOfWeek == DayOfWeek.Saturday) aBuffer++;
if (b.DayOfWeek == DayOfWeek.Sunday) bBuffer--;
if (b.DayOfWeek == DayOfWeek.Saturday) bBuffer++;
return nwdays + aBuffer - bBuffer;
}
{
int nwdays = networkdays(a, b);
int aYear = a.Year;
int bYear = b.Year;
foreach (DateTime holiday in holidays)
{
if (a.CompareTo(holiday) 0) nwdays--;
}
return nwdays;
}
public static int networkdays(DateTime a, DateTime b)
{
int totalDays = ((TimeSpan)b.Subtract(a)).Days;
TimeSpan ts = b.Subtract(a);
int aBuffer = (int)DayOfWeek.Friday - (int)a.DayOfWeek;
int bBuffer = (int)DayOfWeek.Friday - (int)b.DayOfWeek;
int nwdays = (totalDays - aBuffer + bBuffer) * 5 / 7;
if (a.DayOfWeek == DayOfWeek.Sunday) aBuffer--;
if (a.DayOfWeek == DayOfWeek.Saturday) aBuffer++;
if (b.DayOfWeek == DayOfWeek.Sunday) bBuffer--;
if (b.DayOfWeek == DayOfWeek.Saturday) bBuffer++;
return nwdays + aBuffer - bBuffer;
}
If you can make computers as smart as humans you will have invented a machine that can sing the words to the Flintstones tune but will forget to pay the phone bill.