2013-07-22 18 views
2

的列表基于最近的未来日期,我想从天列表获取基于最近的:ASP.NET/C#:从天

方案1:

Date: July 22, 2013 (Monday) 
Possible days: "Tuesday", "Wednesday", "Friday" (string values) 
Answer: July 23, 2013 (Tuesday) 

方案2:

Date: July 23, 2013 (Tuesday) 
Possible days: "Tuesday", "Wednesday", "Thursday", "Saturday" 
Answer: July 24, 2013 (Wednesday) 

方案3:

Date: July 24, 2013 (Wednesday) 
Possible days: "Monday", "Tuesday" 
Answer: July 29, 2013 (Monday) 

有什么建议吗?

+1

最近即将推出的唯一的或过去的一天好? – Ehsan

+0

@EhsanUllah只是未来的日子。 (更新了详细信息) – abramlimpin

+0

请检查第一个场景。 22是星期一,21是星期二,你是怎么得到的? – Uriil

回答

1
DateTime date = DateTime.Parse("July 22, 2013"); 
     DayOfWeek dateDay = date.DayOfWeek; 
     DayOfWeek[] possibleDays = { DayOfWeek.Tuesday,DayOfWeek.Wednesday,DayOfWeek.Friday }; 
     int addToBestAnswer = 7; 
     foreach (var checkDay in possibleDays) 
     { 
      if (checkDay-dateDay<addToBestAnswer) 
      { 
       addToBestAnswer = checkDay - dateDay; 
      } 
     } 
     DateTime Answer = date.AddDays(addToBestAnswer); 

编辑:仅用于字符串输入:

DateTime date = DateTime.Parse("July 22, 2013"); 
     string[] possibleDays={ "Tuesday","Wednesday","Friday" }; 

     List<int> pDays = new List<int>(); 
     foreach (var inputDay in possibleDays) 
     { 
      pDays.Add(int.Parse(inputDay.Replace("Sunday", "0").Replace("Monday", "1").Replace("Tuesday", "2").Replace("Wednesday", "3").Replace("Thursday", "4").Replace("Friday", "5").Replace("Saturday", "6"))); 
     } 
     int dateDay = (int)date.DayOfWeek; 

     int addToBestAnswer = 7; 
     foreach (var checkDay in pDays) 
     { 
      int difference = checkDay - dateDay; 
      if (difference<0) 
      { 
       difference = 7 + difference; 
      } 
      if (difference<addToBestAnswer&&difference!=0) 
      { 
       addToBestAnswer = difference; 
      } 
     } 
     DateTime Answer = date.AddDays(addToBestAnswer); 
     // Answer.ToShortDateString()+" ("+Answer.DayOfWeek.ToString()+")"; 
+0

可能的日子被列为字符串值。 – abramlimpin

+0

为此添加了解决方案 – MMMagic

+0

非常感谢!像魅力一样工作。 – abramlimpin

1

是否这样?

public DateTime GetNextPossibleDay(DateTime DT, DayOfWeek[] PossibleDays) 
    { 
     if (PossibleDays.Length == 0) 
      throw new Exception("No possible day."); 

     do 
     { 
      DT = DT.AddDays(1); 
     } 
     while (!PossibleDays.Contains(DT.DayOfWeek)); 

     return DT; 
    } 
1

您可以检查每个日期在列表中,除非你正确的,是这样的:

var days = new List<string> {"Tuesday", "Monday"}; 
    var startDate = new DateTime(2013, 7, 24).AddDays(1); 
    while (!days.Contains(startDate.DayOfWeek.ToString("G"))) 
    { 
     startDate = startDate.AddDays(1); 
    } 
    Console.WriteLine(startDate); 
1

这应该工作:

var date = new DateTime(2013, 07, 24); 
var posibleDays = new[]{DayOfWeek.Tuesday,DayOfWeek.Wednesday,DayOfWeek.Friday}; 

var nearestDay = posibleDays.Select(dow => new { 
    DayOfWeek = dow, 
    Diff = (7 + (dow - date.DayOfWeek)) % 7 
}) 
.Where(x => x.Diff >= 1) 
.OrderBy(x => x.Diff) 
.FirstOrDefault(); 

灵感来自question of myself一段时间前。如果DayOfWeek的值早于另一个值,它假定在下一周有一天。

这里跟你的样品数据演示:http://ideone.com/VzZnzx

1

测试。此代码的工作

List<DayOfWeek> days = new List<DayOfWeek>() { DayOfWeek.Tuesday, DayOfWeek.Wednesday }; 
     DateTime sourceDate = DateTime.Now; 
     DayOfWeek currentDay = sourceDate.DayOfWeek; 

     int? smallestValue = null; 

     foreach (DayOfWeek d in days) 
     { 
      int currentValue = (int)d - (int)currentDay; 
      if (!smallestValue.HasValue) 
       smallestValue = currentValue; 

      if(smallestValue > currentValue) 
       smallestValue = currentValue; 

     } 

     DateTime nearestDate = sourceDate.AddDays(smallestValue.Value); 
+0

抱歉,我没有收到您的问题 – Ehsan

+0

它仍然建议同一天。 – abramlimpin

+0

你能告诉我你的代码吗? – Ehsan

1

不是花哨LINQ的,但这个工程=)

public static DateTime NearestDate(DateTime baseDateTime, List<string> acceptedDays) 
    { 
     DateTime result = new DateTime(baseDateTime.Year, baseDateTime.Month, baseDateTime.Day); 

     List<DayOfWeek> acceptedDoW = new List<DayOfWeek>(); 
     acceptedDays.ForEach(x => acceptedDoW.Add((DayOfWeek)Enum.Parse(typeof(DayOfWeek), x, true))); 

     DayOfWeek currentDay = baseDateTime.DayOfWeek; 

     int closestDay = int.MaxValue; 

     acceptedDoW.ForEach(x => 
      { 
       int currentSpan = (int)x; 

       if (x < currentDay) 
        currentSpan += 7; 

       currentSpan = currentSpan - (int)currentDay; 

       if (currentSpan < closestDay) 
        closestDay = currentSpan; 
      }); 

     return result.AddDays(closestDay); 
    }