How to get the integer value of day of week

C#DatetimeDayofweekDate Conversion

C# Problem Overview


How do I get the day of a week in integer format? I know ToString will return only 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

C# Solutions


Solution 1 - C#

Use

day1 = (int)ClockInfoFromSystem.DayOfWeek;

Solution 2 - C#

int day = (int)DateTime.Now.DayOfWeek;

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

Solution 3 - C#

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

int day = ((int)DateTime.Now.DayOfWeek == 0) ? 7 : (int)DateTime.Now.DayOfWeek;

Solution 4 - C#

day1= (int)ClockInfoFromSystem.DayOfWeek;

Solution 5 - C#

Try this. It will work just fine:

int week = Convert.ToInt32(currentDateTime.DayOfWeek);

Solution 6 - C#

Another way to get Monday with integer value 1 and Sunday with integer value 7

int day = ((int)DateTime.Now.DayOfWeek + 6) % 7 + 1;

Solution 7 - C#

The correct way to get the integer value of an Enum such as DayOfWeek as a string is:

DayOfWeek.ToString("d")

Solution 8 - C#

The correct answer, is indeed the correct answer to get the int value.

But, if you're just checking to make sure it's Sunday for example... Consider using the following code, instead of casting to an int. This provides much more readability.

if (yourDateTimeObject.DayOfWeek == DayOfWeek.Sunday)
{
	// You can easily see you're checking for sunday, and not just "0"
}

Solution 9 - C#

DateTime currentDateTime = DateTime.Now;
int week = (int) currentDateTime.DayOfWeek;

Solution 10 - C#

Readability counts.

If you need an integer:

int day1 = (int)ClockInfoFromSystem.DayOfWeek;

If you need a string of the weekday integer:

string daystr = $"{(int)ClockInfoFromSystem.DayOfWeek}"; // Unambiguous string of int.

Do not use the recommended ToString conversion, because the majority of programmers are going to have to look it up to make sure that it's a string of the integer and not day of month. Really Microsoft?

string daystr = ClockInfoFromSystem.DayOfWeek.ToString("d"); // Whaa? Horrible! Don't do this.

To change to start of week, add the number of days from Sunday mod 7. Count backwards from Sunday to get the number of days, e.g. 1 back from Sunday is Saturday, 2 back from Sunday is Friday, etc.

int satStart = (int)(ClockInfoFromSystem.DayOfWeek + 1) % 7; // Saturday start
int monStart = (int)(ClockInfoFromSystem.DayOfWeek + 6) % 7; // Monday start

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionakshaykumar6View Question on Stackoverflow
Solution 1 - C#JoeView Answer on Stackoverflow
Solution 2 - C#user1185728View Answer on Stackoverflow
Solution 3 - C#Martin Sansone - MiOEEView Answer on Stackoverflow
Solution 4 - C#FiveToolsView Answer on Stackoverflow
Solution 5 - C#RaviKant HuddaView Answer on Stackoverflow
Solution 6 - C#Ruslan HaydukView Answer on Stackoverflow
Solution 7 - C#Rob SedgwickView Answer on Stackoverflow
Solution 8 - C#Justin J JamesView Answer on Stackoverflow
Solution 9 - C#Michael MurphyView Answer on Stackoverflow
Solution 10 - C#Thane PlummerView Answer on Stackoverflow