在字符串数组中查找下一个可用日期
本文关键字:日期 下一个 查找 字符串 数组 | 更新日期: 2023-09-27 18:27:25
我一直在试图找出如何根据当前日期计算下一个可用日期,即,如果今天是星期五,那么在数组中搜索下一个最近的日期,例如,如果数组值为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
}
}
正如我所说的,基于当前日期,我应该找到下一个可用的日期,即,如果周五,我应该搜索周六,如果周六不在,那么周日、周一等等,直到周五=周五
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 - 用于
startDay
3-输出4 - 对于
startDay
6,它找不到超过6的项,并输出最小值0
对于字符串"5"(仅周五):
- 对于
startDay
5,6-未找到超过5的项,输出最小值(5) - 对于
startDay
0-4-输出5,作为大于0-4的最小数
试试这个;
//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;
}
您应该在int
列表上玩。我将为您提供伪代码。
算法:
- 对可用列表进行排序
- 从列表中获取所有较大的数字(大于当前数字)
- 现在选择最小值,然后中断
- 如果没有更大的数字,请选择所有最小数字(小于当前数字)
- 现在从该列表中选择maximum,然后中断
- 如果没有更多或更少的数字,请选择相同的数字并打断
- 现在将数字转换为星期几
这是我为解决这个问题而提出的可能的算法。您可以将其转换为代码。这可能不是最好的一个,但我相信它会很好用。
检查此项:
Console.WriteLine("Kindly provide input");
string DayInWeek = Console.ReadLine();
string[] GetDays = DayInWeek.Split(',');
Array.Sort(GetDays);
DateTime dt = DateTime.Today;
int i = (int)dt.DayOfWeek;
Console.WriteLine("Today is " + (DayOfWeek)i);
bool flag = false;
foreach (string FirstDay in GetDays)
{
if (Convert.ToInt32(FirstDay) > i)
{
Console.WriteLine((DayOfWeek)Convert.ToInt32(FirstDay) + " is the next day");
flag = true;
break;
}
}
if (!flag)
{
Console.WriteLine((DayOfWeek)Convert.ToInt32(GetDays[0]) + " is the next day");
}
Console.ReadKey();