ASP.NET/C#: Numerical Day of the Week

In ASP.NET/C#, I use this to get the current hour:
Server.Time.ToString("%H")

Open in new window

How can I get the numerical day of the week as a number between 1 and 7 where 1 is Sunday and 7 is Saturday?
LVL 10
skijAsked:
Who is Participating?
 
louisfrCommented:
You can retrieve the DayOfWeek property of any DateTime.
PingPrevious.ClosingTime.DayOfWeek

Open in new window

You can call the ToString method on any object or value
((int)PingPrevious.ClosingTime.DayOfWeek + 1).ToString()

Open in new window

0
 
Shaun KlineLead Software EngineerCommented:
Try:

CINT(Datetime.Now.DayofWeek) + 1
0
 
skijAuthor Commented:
That gives me an error:
Error CS0103: The name 'Datetime' does not exist in the current context

I need something that will work with THIS same type of format:
Server.Time.ToString("%H")

Open in new window

0
 
käµfm³d 👽Commented:
*No Points*

DateTime is in the System namespace, so be sure to bring that in somehow.

e.g.

System.Datetime.Now.DayofWeek + 1

Open in new window


Use the +1 if you want to start at one. The DayOfWeek enum starts its values at zero (i.e. Sunday = 0).
0
 
skijAuthor Commented:
How can I do this without using the system interface?  
                   
For example, sometimes I need to access past dates, not just NOW, like this:
Print(PingPrevious.ClosingTime.ToString("%H"));

I am looking for a way to use this with .ToString(()
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.