Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

type conversion - C# looking for equivalent to DayOfWeek.Tuesday

I would like to change in an existing content one condition to a little bit more flexible and struggel here.:

the original looks like:

if (object.dayofweek == DayOfWeek.Tuesday)
{
}

my first approach was to replace Tuesday by an integer, but this fails. Then I've tried to use:

if (object.dayofweek == Enum.GetName(typeof(DayOfWeek), 2))
{
}

does not work.

Any hint, how I can replace the fixed day by an variable? Thanks!

question from:https://stackoverflow.com/questions/65883290/c-sharp-looking-for-equivalent-to-dayofweek-tuesday

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need a way to get the current day of the week. That could be done with

DateTime.Today.DayOfWeek

If you're using that, then you can keep using DayOfWeek.Tuesday, like this:

if (DateTime.Today.DayOfWeek == DayOfWeek.Tuesday)

And in case you want to know how to change an enum to a variable, use (int)DayOfWeek.Tuesday


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...