在字符串数组中查找下一个可用日期

时间:2015-12-18 06:42:30

标签: c# datetime foreach dayofweek

我一直在试图弄清楚如何根据今天拍摄下一个可用日期,即如果今天是星期五,那么在数组中搜索最近的一天,如果数组值为1 [星期一],2 [星期二] ],4 [星期四],6 [星期六]然后我的第二天应该是星期六。

这是我试过的

    //Here i'll get days like 0, 2, 3, 4, 6 pattern, and i'm spliting them based on comma to get single-single day value in array of string 
string[] GetDays = DayInWeek.Split(','); [Here day patterns will change everytime, based on user selection]

//Here i'm looping to take each day and get Enum Text based on Enum Value
foreach (string FirstDay in GetDays)
{ 
    //Here i'm converting the string value into int and passing to DayOfWeek Enum to get respective day
    DayOfWeek DayChoosen = ((DayOfWeek)(Convert.ToInt32(FirstDay)));

    //Here i have my actual day for example Friday
    DayOfWeek StartDay = "Friday";

    //Here i need condition to find next available day in the foreach i.e., after Friday next value should be Saturday, or Sunday, Monday & so on until Friday==Friday
    if (StartDay == DayChoosen)
    {
        //End foreach
    }
}

正如我在今天所说的那样,我应该找到下一个可用日,如果星期五我应该搜索星期六,如果星期六不在那里星期日,星期一等等,直到星期五=星期五

3 个答案:

答案 0 :(得分:2)

您不需要foreach进行所有这些操作。

您可以执行以下操作:

private int nextDay(string dayInWeek, int startDay) 
{
    int[] getDays = dayInWeek.Split(',').Select(int.Parse).ToArray();   

    return getDays.Where(d => d > startDay).Any()
        ? getDays.Where(d => d > startDay).Min()
        : getDays.Min();
}

此算法会检查您的数组中是否存在一周中的任何几天,并在startDay之后。否则,它会在一周内输出第一天。

例如,对于字符串" 0,2,3,4,6":

  • 表示startDay 0 - 输出2,因为它是大于0的最小整数
  • 表示startDay 1 - 输出2
  • for startDay 3 - output 4
  • 对于startDay 6,它找不到超过6的项目,并且输出最小为0

对于字符串" 5" (仅限星期五):

  • for startDay 5,6 - 找不到超过5的项目,输出最小(5)
  • 表示startDay 0-4 - 输出5,作为最大数字,大于0-4

答案 1 :(得分:0)

试试这个;

    //Here i'll get days like 0, 2, 3, 4, 6 pattern, and i'm splitting them based on comma to get single-single day value in array of string 
    string DayInWeek = "0, 2, 3, 4, 6";
    string[] GetDays = DayInWeek.Split(','); //[Here day patterns will change everytime, based on user selection]

    DayOfWeek nextAvailableDay;

    //Here i'm looping to take each day and get Enum Text based on Enum Value
    foreach (string FirstDay in GetDays)
    {
        //Here i'm converting the string value into int and passing to DayOfWeek Enum to get respective day
        DayOfWeek DayChoosen = ((DayOfWeek)(Convert.ToInt32(FirstDay)));

        //Here i have my actual day for example Friday
        DayOfWeek StartDay = DayOfWeek.Friday;

        //Here i need condition to find next available day in the foreach i.e., after Friday next value should be Saturday, or Sunday, Monday & so on until Friday==Friday
        if (StartDay.Equals(DayChoosen))
                        break;

        if (StartDay < DayChoosen)
        {
            nextAvailableDay = DayChoosen;
            break;
        }
        continue;
    }

答案 2 :(得分:0)

你应该在int列表上玩。我会为你提供伪代码。

<强>算法:

  1. 对可用列表进行排序
  2. 从列表中获取所有更大的数字(大于当前数字)
    1. 现在选择最小值,并打破
  3. 如果您没有更多号码,请选择所有最小号码(小于当前号码)
    1. 现在从此列表中选择最大值,然后中断
  4. 如果您没有更多或更少的数字,请选择相同的数字并打破
  5. 现在将数字转换为工作日
  6. 这是我提出的解决此问题的可能算法。您可以将其转换为代码。可能它不是最好的,但我相信它会正常工作。