简体   繁体   中英

Get day from DateTime using C#

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

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

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);

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

int i = (int)d.DayOfWeek
DateTime.DayOfWeek

是DateTime具有DayOfWeek()方法。

I use:

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

In the public partial class Form :

System.Windows.Forms.Form class 

(For ac# winforms application)

 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)*/

DayOfWeek today = new DateTime().DayOfWeek;

Console.WriteLine(today);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM