Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to find the day number of a week. In the following code the variable 'daynumber' is
showing just zero.In web form variable 'daynumber' works. So I copied
the entire thing in window forms but the 'daynumber' variable shows just zero. How to rectify the code?
C#
DateTime v_todaydt = DateTime.Today;
v_DespatchDate = v_todaydt.AddDays(7).Date.ToString("yyyy/MM/dd");
DateTime dateTime = DateTime.ParseExact(v_DespatchDate, "yyyy/MM/dd", System.Globalization.CultureInfo.InvariantCulture);
var daynumber = ((int)dateTime.DayOfWeek);
Posted
Updated 18-May-14 1:31am
v2
Comments
[no name] 18-May-14 7:20am    
There is nothing to rectify. daynumber is 0 because the day you are looking at this code is Sunday, day 0 of the DayOfWeek enum.
S.Rajendran from Coimbatore 18-May-14 7:31am    
Oh fine. Thanks.

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

Read the Remarks section of the DateTime.DayOfWeek Property[^]

if you need Day number of the week
C#
var daynumber = ((int)dateTime.DayOfWeek) +1;


Side Note: Your code can reduce to
C#
int dayNum = (int)DateTime.Today.AddDays(7).DayOfWeek + 1;
 
Share this answer
 
v4
DayOfWeek is an enumeration.
Go through the example at http://msdn.microsoft.com/en-us/library/system.datetime.dayofweek%28v=vs.110%29.aspx[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900