6

I have a scenario where i want to find the date by subtacting 8 business days from today's date. Suppose if today's date is 04/21/10 .Now i want to show the date to be 04/09/10.Weekends should be excluded.

For Example. If today's date is 04/21/10

Subtract Weekends : Saturday- 04/10/10 ,04/17/10 Sunday-04/11/10,04/18/10

The output comes out to be 04/09/10.

I would like to do this with C#.

Any help or suggestion would be helpful.

Thanks, Sumit

1
  • 7
    What about public holidays?
    – Blorgbeard
    Apr 21, 2010 at 9:49

5 Answers 5

4

There are obviously lots of ways to do this, but maybe have a bit of fun with generators. I've used extension methods but YMMV. So, determine whether you need to make your code cultureally aware (or whatever descriminator you require depending on your needs) etc etc

public static class DateTimeExtensions
{
    public static IEnumerable<DateTime> Forwards(this DateTime dateTime)
    {
        return dateTime.Forwards(TimeSpan.FromDays(1));
    }

    public static IEnumerable<DateTime> Forwards(this DateTime dateTime, TimeSpan span)
    {
        while (true)
        {
            yield return dateTime += span;
        }
    }

    public static IEnumerable<DateTime> Backwards(this DateTime dateTime)
    {
        return dateTime.Backwards(TimeSpan.FromDays(1));
    }

    public static IEnumerable<DateTime> Backwards(this DateTime dateTime, TimeSpan span)
    {
        return dateTime.Forwards(-span);
    }

    public static bool IsWorkingDay(this DateTime dateTime)
    {
        return dateTime.IsWorkingDay(Thread.CurrentThread.CurrentUICulture);
    }

    public static bool IsWorkingDay(this DateTime dateTime, CultureInfo culture)
    {
        return !dateTime.IsWeekend(culture)
            && !dateTime.IsHoliday(culture);
    }

    public static bool IsWeekend(this DateTime dateTime)
    {
        return dateTime.IsWeekend(Thread.CurrentThread.CurrentUICulture);
    }

    public static bool IsWeekend(this DateTime dateTime, CultureInfo culture)
    {
        // TOOD: Make culturally aware

        return dateTime.DayOfWeek == DayOfWeek.Saturday
            || dateTime.DayOfWeek == DayOfWeek.Sunday;
    }

    public static bool IsHoliday(this DateTime dateTime)
    {
        return dateTime.IsHoliday(Thread.CurrentThread.CurrentUICulture);
    }

    public static bool IsHoliday(this DateTime dateTime, CultureInfo culture)
    {
        throw new NotImplementedException("TODO: Get some culture aware holiday data");
    }
}

Then use the DateTime generator to power some LINQ expressions:

        // Display every holiday from today until the end of the year
        DateTime.Today.Forwards()
            .TakeWhile(date => date.Year <= DateTime.Today.Year)
            .Where(date => date.IsHoliday())
            .ForEach(date => Console.WriteLine(date));

You get the picture

0

It is very stupid algorithm but it will work for 8 days. If start day is Mon-Wed (incl.) then add 10 days, else 12. More general is in loop add a day and check is it business day or not.

0

There are just seven cases to consider, so I would calculate how many actual days to subtract depending on the day of the week, something like this (obviously not complete or tested):

switch (dayOfWeek)
{
    case DayOfWeek.Monday :
    case DayOfWeek.Tuesday :
    case DayOfWeek.Wednesday :
        return 12;
    case DayOfWeek.Thursday :
    case DayOfWeek.Friday :
    case DayOfWeek.Saturday :
        return 10;
    case DayOfWeek.Sunday :
        return 11;
}
2
  • You would need to modify this to take into account of public holidays as @blorgbeard said above
    – Aim Kai
    Apr 21, 2010 at 10:23
  • I agree that for many applications it is not enough to just handle weekends. But I understood the question to be how to do that. To handle public holidays, potentially in many different cultures, are of course massively more complex...
    – Peter
    Apr 21, 2010 at 11:30
0
var desiredDate = DateTime.Now.SubtractBusinessDays(8);

by using Fluent DateTime project on Codeplex.

0
    static DateTime GetBusinessDay(int days)
    {
        var dateTime = DateTime.Now;
        bool run = true;

        int i = 0;

        while (run)
        {
            dateTime = dateTime.AddDays(1);

            if (dateTime.DayOfWeek == DayOfWeek.Saturday || dateTime.DayOfWeek == DayOfWeek.Sunday)
            {
                continue;
            }

            i++;

            if (i == 10)
            {
                run = false;
            }
        }

        return dateTime;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.