tags:

views:

126

answers:

8

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.

+2  A: 

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.

Oded
@oded : he return like Sunday,..... or 1,2,3 .. ?
Haim Evgi
@Haim Evgi - It is being cast to an Int32, so it will return a number.
Oded
+5  A: 

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).

astander
+1  A: 

DayOfWeek is based on an enum i.e. 0 based This converts it to a based 1 day number in the week: 1 for Sunday, 2 for Monday...

vc 74
+1  A: 

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 ...

Dewfy
+1  A: 

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.

Hans Kesting
+1  A: 

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.

Andy Rose
+1  A: 

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.

Chris
+1  A: 

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

GôTô