[c#] How to get the integer value of day of week

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

This question is related to c# datetime dayofweek date-conversion

The answer is


Use

day1 = (int)ClockInfoFromSystem.DayOfWeek;

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

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


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"
}

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

DayOfWeek.ToString("d")

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

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

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

Try this. It will work just fine:

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

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;

day1= (int)ClockInfoFromSystem.DayOfWeek;

Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to datetime

Comparing two joda DateTime instances How to format DateTime in Flutter , How to get current time in flutter? How do I convert 2018-04-10T04:00:00.000Z string to DateTime? How to get current local date and time in Kotlin Converting unix time into date-time via excel Convert python datetime to timestamp in milliseconds SQL Server date format yyyymmdd Laravel Carbon subtract days from current date Check if date is a valid one Why is ZoneOffset.UTC != ZoneId.of("UTC")?

Examples related to dayofweek

How to get the integer value of day of week How to get the week day name from a date? How to get the day of week and the month of the year? How to group by week in MySQL?

Examples related to date-conversion

Converting unix time into date-time via excel Conversion failed when converting date and/or time from character string in SQL SERVER 2008 Go / golang time.Now().UnixNano() convert to milliseconds? sql server Get the FULL month name from a date How to convert a string Date to long millseconds Date and time conversion to some other Timezone in java How to get the integer value of day of week Convert one date format into another in PHP