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
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:
kaufmed 👽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

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
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" "">

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
	    <asp:Literal ID="Literal1" runat="server"></asp:Literal>

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.

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

From novice to tech pro — start learning today.