15

Silly question. Given a date in a datetime and I know it's tuesday for instance how do i know its tue=2 and mon=1 etc...

Thanks

abatishchev
  • 98,240
  • 88
  • 296
  • 433
user9969
  • 15,632
  • 39
  • 107
  • 175

10 Answers10

27

You are looking for the DayOfWeek property.
Then, as Dan suggests in the comment below, just look at the integer value of the enum to get the day as integer:

int d = (int)System.DateTime.Now.DayOfWeek
Paolo Tedesco
  • 55,237
  • 33
  • 144
  • 193
  • Sorry i posted the wrong question.Apologies. Given that I know it's thursday how do I map to a number EG monday=1 tue=2 etc.. so given the date I need to know that is monday and =1 how do i do that? – user9969 Jun 07 '10 at 09:59
  • 2
    How do you know Monday == 1? By looking at the enum! – Dan Puzey Jun 07 '10 at 10:14
  • @devnet maybe you should follow the link and read. The clear example shows the usage and output. ("The day of the week for {0:d} is {1}.", dt, dt.DayOfWeek) gives The day of the week for 5/1/2003 is Thursday. – RvdK Jun 07 '10 at 10:21
  • @devnet247: Are you also looking for this: `System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek` – Bobby Jun 07 '10 at 10:28
3

if you want to find out the name of the day, you can do this as follows:

DateTime.Now.DayOfWeek

Response.Write(DateTime.Now.DayOfWeek);
Vijjendra
  • 24,223
  • 12
  • 60
  • 92
3

DayOfWeek is an Enum. To get the integer instead of the string representation you can cast it

int i = (int)d.DayOfWeek
Morten Anderson
  • 2,301
  • 15
  • 20
1

http://msdn.microsoft.com/en-us/library/system.dayofweek.aspx

RPM1984
  • 72,246
  • 58
  • 225
  • 350
1
DateTime.DayOfWeek
Rik
  • 28,507
  • 14
  • 48
  • 67
0

Yes DateTime has DayOfWeek() method.

Arseny
  • 7,251
  • 4
  • 37
  • 52
0

I use:

int day = (int)System.DateTime.Now.DayOfWeek;
string dayoftheweek;

In the public partial class Form :

System.Windows.Forms.Form class 

(For a c# winforms application)

Kenzo_Gilead
  • 2,187
  • 9
  • 35
  • 60
Harry Spruce
  • 15
  • 1
  • 4
0
 DateTime dt = Convert.ToDateTime (txtMaxDate.Value); /*Store date in dt */
int day = dt.Day; /*Get date as (01-01-2019 then it will give 01 as day)*/
Code
  • 679
  • 5
  • 9
0

label3.Text = DateTime.Now.DayOfWeek.ToString();

-1

DayOfWeek today = new DateTime().DayOfWeek;

Console.WriteLine(today);