-2

I am preparing a class which calculates penalty for a library. For example: I have a book. I will return this book 12.11.2009. But I delay this return date. I returned the book on 30.11.2009. I will pay a penalty as money. How can I count (November 31 - November 12) on c#?

Penalty should be calculated for BUSINESS days only. That means my calculation should take account of weekends and national holidays.

    <Holiday Date="25.11.2009"/>
    <Holiday Date="26.11.2009"/>
    <Holiday Date="27.11.2009"/>
Nadir Sampaoli
  • 5,437
  • 4
  • 22
  • 32
cethint
  • 2,231
  • 8
  • 28
  • 31
  • I have no idea. I am new at C#. – cethint Jun 05 '12 at 12:48
  • 4
    You're looking to count the number of business days (non-weekend-days) between 2 dates. - possible duplicate of [Calculate the number of business days between two dates?](http://stackoverflow.com/questions/1617049/calculate-the-number-of-business-days-between-two-dates) – Eoin Campbell Jun 05 '12 at 12:48
  • 4
    I think your first step should be to try and figure it out on your own, and then come back when you have a more concrete problem. Stack Overflow is a good place to help you get around road blocks -- it's not meant to drive the entire distance for you. – ean5533 Jun 05 '12 at 12:49

1 Answers1

2
public int CountBusinessDaysBetween(DateTime start, DateTime end) {
    int days = end.Subtract(start).Days;
    return Enumerable.Range(0, days)
                     .Select(day => start.AddDays(day))
                     .Where(date => date.IsBusinessDay())
                     .Count();
}

Of course, you have to code IsBusinessDay yourself. I don't know what your definition of business day is. It's highly localized.

Also, I don't know if you want to count inclusively or not, so you might need a + 1 on the definition of days if so.

jason
  • 236,483
  • 35
  • 423
  • 525