C# 如何计算从今天开始的8个工作日';谁的日期?

C# 如何计算从今天开始的8个工作日';谁的日期?,c#,asp.net,C#,Asp.net,我有一个场景,我想通过从今天的日期算起8个工作日来查找日期。假设今天的日期是04/21/10。现在我想显示日期是04/09/10。周末应该被排除在外 比如说。 如果今天的日期是2010年4月21日 减去周末: 星期六-2010年10月4日,2010年4月17日 2010年11月4日星期日,2010年4月18日 输出结果显示为04/09/10 我想用C做这个 任何帮助或建议都会有帮助 谢谢, Sumit这是一个非常愚蠢的算法,但它可以工作8天。如果开始日为周一至周三(包括),则添加10天,否则12

我有一个场景,我想通过从今天的日期算起8个工作日来查找日期。假设今天的日期是04/21/10。现在我想显示日期是04/09/10。周末应该被排除在外

比如说。 如果今天的日期是2010年4月21日

减去周末: 星期六-2010年10月4日,2010年4月17日 2010年11月4日星期日,2010年4月18日

输出结果显示为04/09/10

我想用C做这个

任何帮助或建议都会有帮助

谢谢,
Sumit

这是一个非常愚蠢的算法,但它可以工作8天。如果开始日为周一至周三(包括),则添加10天,否则12天。更一般的是在循环中添加一天,并检查是否为工作日

>P>有七种情况要考虑,所以我将根据实际情况来计算多少天减去,比如这样的(显然没有完成或测试):

通过使用Codeplex上的project。

显然有很多方法可以做到这一点,但也许可以从生成器中获得一些乐趣。我使用了扩展方法,但没有使用YMMV。因此,确定是否需要使代码具有文化意识(或根据需要使用任何描述符),等等

公共静态类DateTimeExtensions
{
公共静态IEnumerable转发(此日期时间日期时间)
{
返回日期时间转发(TimeSpan.FromDays(1));
}
公共静态IEnumerable转发(此日期时间日期时间,时间跨度)
{
while(true)
{
收益返回日期时间+=跨度;
}
}
公共静态IEnumerable向后(此日期时间日期时间)
{
返回日期时间向后(TimeSpan.FromDays(1));
}
公共静态IEnumerable向后(此日期时间日期时间,时间跨度)
{
返回日期时间。转发(-span);
}
公共静态布尔值为工作日(此日期时间日期时间)
{
返回日期时间.IsWorkingDay(Thread.CurrentThread.CurrentUICulture);
}
公共静态bool IsWorkingDay(此日期时间日期时间,文化信息文化)
{
return!dateTime.IsWeekend(文化)
&&!dateTime.IsHoliday(文化);
}
公共静态bool IsWeekend(此日期时间日期时间)
{
return dateTime.IsWeekend(Thread.CurrentThread.CurrentUICulture);
}
公共静态bool IsWeekend(此日期时间日期时间,文化信息文化)
{
//嘟嘟:使文化意识
return dateTime.DayOfWeek==DayOfWeek.周六
||dateTime.DayOfWeek==DayOfWeek.Sunday;
}
公共静态bool IsHoliday(此日期时间日期时间)
{
return dateTime.IsHoliday(Thread.CurrentThread.CurrentUICulture);
}
公共静态bool IsHoliday(此日期时间日期时间,文化信息文化)
{
抛出新的NotImplementedException(“TODO:获取一些有文化意识的假日数据”);
}
}
然后使用DateTime生成器为某些LINQ表达式提供电源:

        // 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));
//显示从今天到年底的每个假日
DateTime.Today.Forwards()
.TakeWhile(date=>date.Year-date.IsHoliday())
.ForEach(日期=>Console.WriteLine(日期));

你明白了

关于公共假日呢?你需要修改此选项以考虑到@blorgbeard在上面所说的公共假日。我同意,对于许多应用程序来说,仅仅处理周末是不够的。但我明白问题是如何做到这一点。处理公共假期,可能在许多不同的文化中,当然要复杂得多。。。
    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;
    }
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");
    }
}
        // 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));
    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;
    }