在字符串数组中查找下一个可用日期
Find next available day in an Array of string
我一直在尝试找出如何根据当前日期获取下一个可用日期,即,如果今天是星期五,则在数组中搜索下一个最近的日期,例如如果数组值为 1[Monday], 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
}
}
正如我所说的,根据现在的日期,我应该找到下一个可用的日期,即,如果星期五,我应该搜索星期六,如果星期六不存在,那么星期日、星期一等等,直到星期五=星期五
试试这个;
//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;
}
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”:
- for
startDay
0 - 输出 2,因为它是大于 0 的最小整数
- 对于
startDay
1 - 输出 2
- 对于
startDay
3 - 输出 4
- for
startDay
6 它没有找到超过 6 的项目,并输出最小值 0
对于字符串“5”(仅限星期五):
- for
startDay
5, 6 - 没有找到超过 5 的项目,输出最小值 (5)
- for
startDay
0-4 - 输出 5,作为大于 0-4 的最小数
你应该在 int
名单上玩。我给你伪代码。
算法:
- 对可用列表进行排序
- 从列表中获取所有更大的数字(大于你当前的数字)
- select现在最小,突破
- 如果您没有更大的数字,select所有最小数字(小于您当前的数字)
- select 现在这个列表的最大值,然后打破
- 如果你没有任何更大或更小的数字,比 select 相同的数字和中断
- 现在将数字转换为工作日
这是我想出的解决这个问题的可能算法。您可以将其转换为代码。可能不是最好的,但我相信它会很好用。
检查这个:
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();
我一直在尝试找出如何根据当前日期获取下一个可用日期,即,如果今天是星期五,则在数组中搜索下一个最近的日期,例如如果数组值为 1[Monday], 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
}
}
正如我所说的,根据现在的日期,我应该找到下一个可用的日期,即,如果星期五,我应该搜索星期六,如果星期六不存在,那么星期日、星期一等等,直到星期五=星期五
试试这个;
//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;
}
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”:
- for
startDay
0 - 输出 2,因为它是大于 0 的最小整数
- 对于
startDay
1 - 输出 2 - 对于
startDay
3 - 输出 4 - for
startDay
6 它没有找到超过 6 的项目,并输出最小值 0
对于字符串“5”(仅限星期五):
- for
startDay
5, 6 - 没有找到超过 5 的项目,输出最小值 (5) - for
startDay
0-4 - 输出 5,作为大于 0-4 的最小数
你应该在 int
名单上玩。我给你伪代码。
算法:
- 对可用列表进行排序
- 从列表中获取所有更大的数字(大于你当前的数字)
- select现在最小,突破
- 如果您没有更大的数字,select所有最小数字(小于您当前的数字)
- select 现在这个列表的最大值,然后打破
- 如果你没有任何更大或更小的数字,比 select 相同的数字和中断
- 现在将数字转换为工作日
这是我想出的解决这个问题的可能算法。您可以将其转换为代码。可能不是最好的,但我相信它会很好用。
检查这个:
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();