1

I'd like to know if exists any library / method that allows to sum days to a datetime without consider the "no work days" like Easter, Christmas, Saturdays and so on..

I know that exists the method DateTime.Add, but this didn't consider "non working days".

Thank you very much!

Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
Ziba Leah
  • 2,484
  • 7
  • 41
  • 60
  • 2
    That seems rather locale-specific... Different countries (and indeed, different cities in some cases) have different public holidays. For that reason, I'd think it unlikely that there's a general purpose library for it. You'd need to supply a list of holidays to any general purpose library, and if you have that list available then what you want to do is trivial in any case. – Matthew Watson Feb 20 '13 at 15:17
  • I have worked on Saturdays.. – Soner Gönül Feb 20 '13 at 15:18
  • 1
    How was the last comment useful? Anyway @Ziba Leah: You'll have write your own algorythm for that. We do a lot of date manipulations and I don't know of a built in method that doed what you're after since as ohters suggested it is locale-specific. – Risho Feb 20 '13 at 15:22

4 Answers4

3

You can use the TimeSpan structure for time intervals. Here is an example on another stackoverflow thread:

Calculate the number of business days between two dates?

MSDN Documentation for TimeSpan

Community
  • 1
  • 1
Tarzan
  • 4,270
  • 8
  • 50
  • 70
  • +1 Nice link. Quite efficient unless the number of holidays in the bankHolidays list is long compared to the date range, in which case it becomes a bit slower. – Matthew Watson Feb 20 '13 at 15:35
0

No, you'd have to define your own non-work days and build your own function.

MrFox
  • 4,852
  • 7
  • 45
  • 81
0

There is a DateTime method that will tell you the day of the week, so you can check for Sat/Sun but the rest is up to you. If you are going to be using it with Office there are some dlls in there that could be used. Otherwise you will probably want to find a open-source project (lots of them out there) that can calculate that out for you.

DateTime.Now.DayOfWeek

It is going to be very region specific, so if you are planning on using your code in more than one country that is something to consider. Also, in the US, different states have different holidays too.

Anthony Nichols
  • 1,586
  • 2
  • 25
  • 50
0

I'm assuming you want to find the end date for something given a start date and the number of working days that it will take?

If so, this the following code may be what you want. It assumes that you have a list of all the holidays for the maximum range of time that you are supporting. That could be a few years in the future, depending on your requirements!

This code takes the start date and the number of working days (and the list of holidays) and returns the end date.

public static DateTime AddWorkingDays(DateTime start, int workingDays, IEnumerable<DateTime> holidays)
{
    var dict = new HashSet<DateTime>(holidays);
    DateTime date;

    for (date = start; workingDays > 0; date = date.AddDays(1))
    {
        if (!dict.Contains(date))
        {
            --workingDays;
        }
    }

    return date;
}

An alternative implementation assumes that you have a predicate you can call to determine if a day is a holiday:

public static DateTime AddWorkingDays(DateTime start, int workingDays, Predicate<DateTime> isHoliday)
{
    DateTime date;

    for (date = start; workingDays > 0; date = date.AddDays(1))
    {
        if (!isHoliday(date))
        {
            --workingDays;
        }
    }

    return date;
}

Both of these implementations suffer from visiting every date. This is possibly not an issue, but if the date range is large it could be too slow.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276