Haim Evgi
Haim Evgi

Reputation: 125524

what this line do in c#

i want to understand a line in c#,

Int32 dayOfWeekIndex = (Int32)DateTime.Now.DayOfWeek + 1;

what this return for example if we run it today ?

I do not have the option to run this code.

Upvotes: 0

Views: 191

Answers (7)

GôTô
GôTô

Reputation: 8053

0 is sunday, 6 is saturday, so if you run the code today (thursday), you'll get 5

http://msdn.microsoft.com/en-us/library/system.datetime.dayofweek%28v=VS.80%29.aspx

Upvotes: 1

Chris
Chris

Reputation: 27609

http://msdn.microsoft.com/en-us/library/system.datetime.dayofweek.aspx

An enumerated constant that indicates the day of the week of this DateTime value.

The value of the constants in the DayOfWeek enumeration ranges from DayOfWeek.Sunday to DayOfWeek.Saturday. If cast to an integer, its value ranges from zero (which indicates DayOfWeek.Sunday) to six (which indicates DayOfWeek.Saturday).

SO today being thursday means that DayOfWeek will be 4. So day of week+1 will be 5.

I did not run this code. I just used google and msdn.

Upvotes: 1

Andy Rose
Andy Rose

Reputation: 16984

DateTime.DayOfWeek is an enum for the days of the week with values such as DayOfWeek.Monday, DayOfWeek.Tuesday etch. It's underlying type is integers which is the default for enums. Therefore this code will return the underlying integer for whichever day it current is plus one.

Upvotes: 1

Hans Kesting
Hans Kesting

Reputation: 39329

DateTime.Now returns the current date. The DayOfWeek property returns the day-of-the-week (monday, tuesday, etc) of that date as an enum value.

The cast to Int32 turns that enum value into an int (where sunday = 0).

Then 1 is added so sunday will end up as 1 and thursday as 5.

Upvotes: 1

Dewfy
Dewfy

Reputation: 23624

According to MSDN: "The value of the constants in the DayOfWeek enumeration ranges from DayOfWeek.Sunday to DayOfWeek.Saturday. If cast to an integer, its value ranges from zero"

So you see usage of enumeration that is casted to int. For Sunday + 1 = 1 ...

Upvotes: 1

Adriaan Stander
Adriaan Stander

Reputation: 166416

Google is your friend X-)

See DayOfWeek Enumeration

If cast to an integer, its value ranges from zero (which indicates DayOfWeek.Sunday) to six (which indicates DayOfWeek.Saturday).

Upvotes: 5

Oded
Oded

Reputation: 499092

It depends on where in the world you are and what your computer clock is set up to.

It returns the value of the DayOfWeek enum corresponding to the time it was run, plus 1. Sunday is 0, Saturday is 6.

So, for Thursday, dayOfWeekIndex would be 5.

Upvotes: 2

Related Questions