如何检查日期在两个给定日期之间
How to check date day is between two given days
我必须检查我的特定日期是否在两个字符串日期之间或等于它们
例如:
var startDay = "Saturday";
var endDay = "Tuesday";
DateTime myDate = DateTime.Now;
if(myDate.DayofWeek >= startDay && myDate.DayofWeek <= endDay){
//some code here...
}
您可以使用 DayOfWeek
枚举。
if(myDate.DayOfWeek >= DayOfWeek.Tuesday
&& myDate.DayOfWeek <= DayOfWeek.Saturday)
{
// This would catch the days from Tuesday to Saturday
}
如果你想赶上从星期六到星期二的日子,你必须稍微更改一下你的代码。
if(myDate.DayOfWeek >= DayOfWeek.Saturday
|| myDate.DayOfWeek <= DayOfWeek.Tuesday)
{
// This would catch the days from Tuesday to Saturday
}
如果您的 startDate
和 endDate
正在更改。然后你需要解析第一个并做一些额外的检查。
DayOfWeek startDayOfWeek;
DayOfWeek endDayOfWeek;
if (!Enum.TryParse(startDay, out startDayOfWeek))
{
// Something wrong happened and you have to handle it.
}
if (!Enum.TryParse(endDay, out endDayOfWeek))
{
// Something wrong happened and you have to handle it.
}
if (
((int)startDayOfWeek < (int)endDayOfWeek
&& myDate.DayOfWeek >= startDayOfWeek
&& myDate.DayOfWeek <= endDayOfWeek)
|| ((int)startDayOfWeek > (int)endDayOfWeek
&& (myDate.DayOfWeek >= startDayOfWeek
|| myDate.DayOfWeek <= endDayOfWeek))
)
{
}
您可以将这些字符串解析为 DayOfWeek
枚举:
var startDay = "Saturday";
var endDay = "Tuesday";
DayOfWeek startDayOfWeek, endDayOfWeek;
if (!Enum.TryParse(startDay, out startDayOfWeek))
// error handlnig
if (!Enum.TryParse(endDay, out endDayOfWeek))
// error handlnig
DateTime myDate = DateTime.Now;
if(myDate.DayofWeek >= startDayOfWeek && myDate.DayofWeek <= endDayOfWeek){
//some code here...
}
但这会带来另一个问题:DayOfWeek
的值从星期日 (0) 到星期六 (6)。根据您定义的一周开始时间或 between 的含义,您可能需要调整这些值。
所以这里有一个建议:
int startDayAsInt = (int)startDayOfWeek; // the parsed value from above
int endDayAsInt = (int)endDayOfWeek;
int myDateAsInt = (int)myDate.DayOfWeek;
if (endDayAsInt < startDayAsInt) endDayAsInt += 7;
if (myDateAsInt < startDayAsInt) myDateAsInt += 7;
if (myDateAsInt >= startDayAsInt && myDateAsInt <= endDayAsInt)
// do something
这应该适用于所有组合,因为它会将 "next" 周的天数投射到开始日之前。
我必须检查我的特定日期是否在两个字符串日期之间或等于它们 例如:
var startDay = "Saturday";
var endDay = "Tuesday";
DateTime myDate = DateTime.Now;
if(myDate.DayofWeek >= startDay && myDate.DayofWeek <= endDay){
//some code here...
}
您可以使用 DayOfWeek
枚举。
if(myDate.DayOfWeek >= DayOfWeek.Tuesday
&& myDate.DayOfWeek <= DayOfWeek.Saturday)
{
// This would catch the days from Tuesday to Saturday
}
如果你想赶上从星期六到星期二的日子,你必须稍微更改一下你的代码。
if(myDate.DayOfWeek >= DayOfWeek.Saturday
|| myDate.DayOfWeek <= DayOfWeek.Tuesday)
{
// This would catch the days from Tuesday to Saturday
}
如果您的 startDate
和 endDate
正在更改。然后你需要解析第一个并做一些额外的检查。
DayOfWeek startDayOfWeek;
DayOfWeek endDayOfWeek;
if (!Enum.TryParse(startDay, out startDayOfWeek))
{
// Something wrong happened and you have to handle it.
}
if (!Enum.TryParse(endDay, out endDayOfWeek))
{
// Something wrong happened and you have to handle it.
}
if (
((int)startDayOfWeek < (int)endDayOfWeek
&& myDate.DayOfWeek >= startDayOfWeek
&& myDate.DayOfWeek <= endDayOfWeek)
|| ((int)startDayOfWeek > (int)endDayOfWeek
&& (myDate.DayOfWeek >= startDayOfWeek
|| myDate.DayOfWeek <= endDayOfWeek))
)
{
}
您可以将这些字符串解析为 DayOfWeek
枚举:
var startDay = "Saturday";
var endDay = "Tuesday";
DayOfWeek startDayOfWeek, endDayOfWeek;
if (!Enum.TryParse(startDay, out startDayOfWeek))
// error handlnig
if (!Enum.TryParse(endDay, out endDayOfWeek))
// error handlnig
DateTime myDate = DateTime.Now;
if(myDate.DayofWeek >= startDayOfWeek && myDate.DayofWeek <= endDayOfWeek){
//some code here...
}
但这会带来另一个问题:DayOfWeek
的值从星期日 (0) 到星期六 (6)。根据您定义的一周开始时间或 between 的含义,您可能需要调整这些值。
所以这里有一个建议:
int startDayAsInt = (int)startDayOfWeek; // the parsed value from above
int endDayAsInt = (int)endDayOfWeek;
int myDateAsInt = (int)myDate.DayOfWeek;
if (endDayAsInt < startDayAsInt) endDayAsInt += 7;
if (myDateAsInt < startDayAsInt) myDateAsInt += 7;
if (myDateAsInt >= startDayAsInt && myDateAsInt <= endDayAsInt)
// do something
这应该适用于所有组合,因为它会将 "next" 周的天数投射到开始日之前。