我有一个场景,我想从今天的日期起8个工作日内找到日期。假设今天的日期是04/21/10。现在我想显示日期是04/09/10。应该排除期望值。如何计算今天的8个工作日?
例如。 如果今天的日期是10年4月21日
减去周末: Saturday- 04/10/10,04 /10分之17 周日-04/11/10,04/18/10
的产量是04/09/10。
我想用C#做到这一点。
任何帮助或建议将有所帮助。
感谢, 萨米特
我有一个场景,我想从今天的日期起8个工作日内找到日期。假设今天的日期是04/21/10。现在我想显示日期是04/09/10。应该排除期望值。如何计算今天的8个工作日?
例如。 如果今天的日期是10年4月21日
减去周末: Saturday- 04/10/10,04 /10分之17 周日-04/11/10,04/18/10
的产量是04/09/10。
我想用C#做到这一点。
任何帮助或建议将有所帮助。
感谢, 萨米特
你可以在这里找到一些很好的解释
http://codeasp.net/blogs/Vijjendra/microsoft-net/765/calculate-business-days-in-asp-net-c
谢谢vijjendra多数民众赞成什么我想要做什么。 – Sumit 2010-10-17 18:00:37
这是非常愚蠢的算法,但它会8天的工作。如果开始日期是星期一至星期三(含),则添加天,否则。更一般的是循环添加一天,检查是否是营业日。
看一看这篇文章也可能是使用的你
有短短七年情况需要考虑,所以我会计算有多少实际的天视的一天减本周,像这样(显然不完全或测试):使用Codeplex上Fluent DateTime项目
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;
}
您需要对此进行修改以考虑公众假期,因为@ blorgbeard上面说的 – 2010-04-21 10:23:21
我同意,对于许多应用程序来说,仅仅处理周末是不够的。但我明白这个问题是如何做到的。处理公共假期,可能在许多不同的文化中,当然是非常复杂... – Peter 2010-04-21 11:30:30
var desiredDate = DateTime.Now.SubtractBusinessDays(8);
。
明显有很多的方法来做到这一点,但也许有一些发电机的乐趣。我已经使用扩展方法,但YMMV。因此,确定是否需要让你的代码cultureally知道(或者您需要根据您的一切需求descriminator)等等等等
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");
}
}
然后使用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));
你得到的图片
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;
}
公共假日呢? – Blorgbeard 2010-04-21 09:49:16