.
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
를 변경하는 경우. 그런 다음 첫 번째 구문을 분석하고 몇 가지 추가 검사를 수행해야합니다.
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 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))
)
{
}
정말요? 그 중 하나는 OP가해야 할 한 가지 확실한 열거 형으로 문자열을 변환하는 것입니다. 그러나 여기서 질문은 두 평일 사이에 날짜가 있는지 파악하는 것입니다. – juunas