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

Using C#,  I want the day number of the week.  On Monday it should be 2.  On Saturday it should be 7.
Console.WriteLine( Server.Time.ToString("?") );

Open in new window

LVL 10
skijAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
(int)DateTime.Now.DayOfWeek
0
käµfm³d 👽Commented:
What is the data type of the Time property? If it's DateTime, then you could do:

Console.WriteLine((((int)Server.Time.DayOfWeek) + 1));

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
it_saigeDeveloperCommented:
Well the DayOfWeek enumeration is what is normally used.  But it is zero based; e.g. -

Default.aspx.cs -
using System;

namespace EE_Q28683320
{
	public partial class _Default : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			DateTime date = DateTime.Now;
			Literal1.Text = String.Format("{0} = {1}", date.DayOfWeek, Convert.ToInt16(date.DayOfWeek));
			Response.Write("The DayOfWeek Enumeration contains the following values:");
			Response.Write("<br />");
			foreach (var item in Enum.GetValues(typeof(DayOfWeek)))
			{
				Response.Write(string.Format("{0} = {1}", item, Convert.ToInt16(item)));
				Response.Write("<br />");
			}
			Response.Write("<br />");
		}
	}
}

Open in new window

Default.aspx -
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="EE_Q28683320._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
	    <asp:Literal ID="Literal1" runat="server"></asp:Literal>
    </div>
    </form>
</body>
</html>

Open in new window

Produces the following output -Capture.JPG
So as Kaufmed stated, just add 1 to its value.  But be wary of this if you do a reverse conversion.

-saige-
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.