133

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
user2771704
  • 5,994
  • 6
  • 37
  • 38
akshaykumar6
  • 2,147
  • 4
  • 18
  • 31

10 Answers10

207

Use

day1 = (int)ClockInfoFromSystem.DayOfWeek;
Joe
  • 41,484
  • 20
  • 104
  • 125
106
int day = (int)DateTime.Now.DayOfWeek;

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

peroija
  • 1,982
  • 4
  • 21
  • 37
81

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;
Martin Sansone - MiOEE
  • 4,281
  • 1
  • 29
  • 31
  • 14
    Up-voted as all sane people would want this. ISO 8601 is quite clear on the point. Monday is day 1, Sunday is day 7. – Morvael Aug 18 '16 at 10:30
  • Didn't release the DayOfWeek property would return zero on sunday. Almost introduced a bug by implementing the top voted answer. Thanks! – brz Jul 18 '19 at 07:32
6

Try this. It will work just fine:

int week = Convert.ToInt32(currentDateTime.DayOfWeek);
Jarosław Kończak
  • 3,387
  • 2
  • 19
  • 36
RaviKant Hudda
  • 1,042
  • 10
  • 25
6

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

DayOfWeek.ToString("d")
Rob Sedgwick
  • 4,342
  • 6
  • 50
  • 87
6
day1= (int)ClockInfoFromSystem.DayOfWeek;
FiveTools
  • 5,970
  • 15
  • 62
  • 84
  • @JustinSatyr - FiveTools was clearly just giving an example. It is not his fault the author was attempting to set a String value to an integer variable. – Security Hound Feb 08 '12 at 18:23
  • @Ramhound: I didn't downvote and I wasn't criticizing. I thought he misread the code. And apparently I was correct by the fact that he corrected his code. – Devin Burke Feb 08 '12 at 18:27
5

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

int day = ((int)DateTime.Now.DayOfWeek + 6) % 7 + 1;
Ruslan Hayduk
  • 51
  • 2
  • 2
2

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"
}
0
DateTime currentDateTime = DateTime.Now;
int week = (int) currentDateTime.DayOfWeek;
Michael Murphy
  • 1,921
  • 2
  • 18
  • 21
0

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
Thane Plummer
  • 7,966
  • 3
  • 26
  • 30