Get day from DateTime using C#
Asked Answered
D

10

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

Daphne answered 7/6, 2010 at 9:38 Comment(0)
L
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
Lyon answered 7/6, 2010 at 9:41 Comment(4)
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?Daphne
How do you know Monday == 1? By looking at the enum!Burchette
@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.Testify
@devnet247: Are you also looking for this: System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeekMagallanes
K
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);
Kratzer answered 7/6, 2010 at 9:42 Comment(1)
DateTime.Now.DayOfWeek.ToString() will give name of daySpahi
P
3

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

int i = (int)d.DayOfWeek
Polarimeter answered 7/6, 2010 at 10:7 Comment(0)
U
1
DateTime.DayOfWeek
Unchancy answered 7/6, 2010 at 9:40 Comment(0)
S
0

Yes DateTime has DayOfWeek() method.

Sugden answered 7/6, 2010 at 9:40 Comment(1)
It is not a method, it is a property and can be accessed like the other answers.. :-)Cal
C
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)

Codfish answered 30/6, 2017 at 13:31 Comment(0)
H
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)*/
Hazelton answered 15/3, 2019 at 5:39 Comment(1)
please explain why this snippet solves the problem, and look at How to AnswerSandberg
E
0

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

Exum answered 30/9, 2022 at 0:15 Comment(0)
O
-1

DayOfWeek today = new DateTime().DayOfWeek;

Console.WriteLine(today);

Ogle answered 3/1, 2022 at 11:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.