Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 718
user9969 Avatar asked Jun 07 '10 09:06

user9969


People also ask

How to get day today in C#?

C# today's date Now; Console. WriteLine(now. ToString("F")); The example prints today's date.

How do I convert DateTime to TimeSpan?

To convert a DateTime to a TimeSpan you should choose a base date/time - e.g. midnight of January 1st, 2000, and subtract it from your DateTime value (and add it when you want to convert back to DateTime ). If you simply want to convert a DateTime to a number you can use the Ticks property. Save this answer.


3 Answers

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
like image 137
Paolo Tedesco Avatar answered Oct 21 '22 11:10

Paolo Tedesco


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

int i = (int)d.DayOfWeek
like image 20
Morten Anderson Avatar answered Oct 21 '22 11:10

Morten Anderson


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);
like image 34
Vijjendra Avatar answered Oct 21 '22 10:10

Vijjendra