2010-04-21 96 views
6

我有一個場景,我想從今天的日期起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#做到這一點。

任何幫助或建議將有所幫助。

感謝, 薩米特

+7

公共假日呢? – Blorgbeard 2010-04-21 09:49:16

回答

0

這是非常愚蠢的算法,但它會8天的工作。如果開始日期是星期一至星期三(含),則添加天,否則。更一般的是循環添加一天,檢查是否是營業日。

0

有短短七年情況需要考慮,所以我會計算有多少實際的天視的一天減本週,像這樣(顯然不完全或測試):使用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; 
} 
+0

您需要對此進行修改以考慮公衆假期,因爲@ blorgbeard上面說的 – 2010-04-21 10:23:21

+0

我同意,對於許多應用程序來說,僅僅處理週末是不夠的。但我明白這個問題是如何做到的。處理公共假期,可能在許多不同的文化中,當然是非常複雜... – Peter 2010-04-21 11:30:30

0
var desiredDate = DateTime.Now.SubtractBusinessDays(8); 

4

明顯有很多的方法來做到這一點,但也許有一些發電機的樂趣。我已經使用擴展方法,但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)); 

你得到的圖片

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; 
    }