I have to check that my specific date day is between two string days or equals to them For instance:
var startDay = "Saturday";
var endDay = "Tuesday";
DateTime myDate = DateTime.Now;
if(myDate.DayofWeek >= startDay && myDate.DayofWeek <= endDay){
//some code here...
}
You can parse those strings to a DayOfWeek
enum:
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...
}
But that gives another problem: the values of DayOfWeek
go from sunday (0) to saturday (6). And depending on what you define as start of your week or what between means, you might need to adjust the values.
So here is a suggestion:
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
This should work for all combinations as it projects days into the "next" week if they are before the start day.