How to get the integer value of the day of the week

How to get the day of the week in integer format? I know that ToString will only return a string.

DateTime ClockInfoFromSystem = DateTime.Now; int day1; string day2; day1= ClockInfoFromSystem.DayOfWeek.ToString(); /// it is not working day2= ClockInfoFromSystem.DayOfWeek.ToString(); /// it gives me string 
+62
c # datetime date-conversion dayofweek
Feb 08 '12 at 18:14
source share
7 answers

Using

 day1 = (int)ClockInfoFromSystem.DayOfWeek; 
+110
Feb 08 '12 at 18:17
source share
 int day = (int)DateTime.Now.DayOfWeek; 

First day of the week: Sunday (with a value of 0)

+50
Feb 08 2018-12-12T00:
source share

If you want to set the first day of the week on Monday with integer value 1 and Sunday with integer value 7

 int day = ((int)DateTime.Now.DayOfWeek == 0) ? 7 : (int)DateTime.Now.DayOfWeek; 
+35
Dec 30 '14 at 2:49
source share
 day1= (int)ClockInfoFromSystem.DayOfWeek; 
+6
Feb 08 '12 at 18:17
source share

Try it. This will work just fine:

 int week = Convert.ToInt32(currentDateTime.DayOfWeek); 
+4
Sep 25 '14 at 10:25
source share

The correct way to get an integer value Enum, such as DayOfWeek, as a string:

 DayOfWeek.ToString("d") 
+2
Dec 24 '14 at 9:41
source share
 DateTime currentDateTime = DateTime.Now; int week = (int) currentDateTime.DayOfWeek; 
0
Mar 11 '14 at 21:26
source share



All Articles