Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate 8 business days from today's date?

Tags:

c#

asp.net

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

like image 546
Sumit Avatar asked Apr 21 '10 09:04

Sumit


1 Answers

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

like image 145
Kevin Nixon Avatar answered Oct 21 '22 19:10

Kevin Nixon