OK I had C/C++ programming classes a few years ago and now I'm taking a ASP.net class and programming in C#. I need a little help with an assingment. We have to create an if state to display text depending on if the day of the week is a weekday vs weekend. I was able to get the time to dispaly ok. This next step I was taking before I created my if conditions was to try and display the day of the Week. Which I am unable to do. Any help would be appreciated. Also I don't have a compiler to test my code. I was trying to view the errors in IE. I added a web.config file to the root of my directory but it still does not give my an error description. Not sure if I have it in the correct place.

Here is the code for my program:

[

<script Language="c#" runat="server">
  void Page_Load() 
  {
    
    DateTime CurrentTime;
    DateTime MyTime;
    CurrentTime = DateTime.Now;
    MyTime = DayOfWeek.Now;
    message1.Text = "<br>The current time is:  " + CurrentTime;
    message2.Text = "<br>The Day of the week is: "+ MyTime.DayOfWeek.ToString();
  }
 
</script>
<html>
<head><title>Assignment2</title></head>
<body>
   <h1>Assignment 2</h1>
       Cassandra Jackson ECT 310</a>
   <asp:label id="message1" runat="server"/>
   <asp:label id="message2" runat="server"/>
                              
</body>
 
</html>

Dani AI

Generated

The compile error came from a type mix-up: DayOfWeek is an enum (System.DayOfWeek), not a type with a Now property. As pointed out, assign a DateTime (for example DateTime.Now or DateTime.Today) and then read its DayOfWeek property. ’s explicit weekend check (testing Saturday/Sunday) is perfectly valid; an alternative is a range check that treats Monday–Friday as weekdays.

A compact, culture-aware way to show the full day name and decide weekday vs weekend:

// localized full day name, e.g. "Monday"
string dayName = DateTime.Now.ToString("dddd");

// weekday test using DayOfWeek enum values
bool isWeekday = DateTime.Now.DayOfWeek >= DayOfWeek.Monday && DateTime.Now.DayOfWeek <= DayOfWeek.Friday;

// example output to a label control
dayLabel.Text = "<br>Today is " + dayName + " — " + (isWeekday ? "a weekday." : "the weekend.");

Notes and troubleshooting:

  • ’s snippet looks like VB-style usage; in C# DayOfWeek is a property (no parentheses) on a DateTime instance.
  • If you only care about the date part use DateTime.Today (time component will be midnight).
  • To see compile/runtime errors in the browser while developing, place web.config in the web app root (the application’s virtual directory) and enable debugging and error pages during development:
<system.web>
  <compilation debug="true" />
  <customErrors mode="Off" />
</system.web>

Remember to turn off debug/custom error display in production. If you still don’t see errors, confirm the folder is configured as an IIS application (or use Visual Studio/ASP.NET dev server) so ASP.NET actually reads your web.config.

Recommended Answers

All 5 Replies

Shouldn't MyTime = DayOfWeek.Now; be something like MyTime = (DateTime.Now); . Then you should be able to do MyTime.DayOfWeek since DayOfWeek is a property of the DateTime class, so should be called on an instance variable of that class.

I havent fiddled too much with the DateTime mechanism yet in C#, but this seems like it should work.

Regards,

Tyler S. Breton

Thanks A Bunch It Worked.

you can do as follows:


mydate=system.date.now.date;
date sdate;
sdate=mydate.dayofweek(); /////it will return actual day e.g.
sunday, monday,etc
if (dayofweek==weekday)
/////do some thing

try it
best of luck

Try this:

public static bool IsWeekday(DateTime dtInput)
        {
            return ((dtInput.DayOfWeek == DayOfWeek.Saturday) || (dtInput.DayOfWeek == DayOfWeek.Sunday)) ? false : true;
        }

If it's a weekday, it returns true, otherwise false.

Try this:

public static bool IsWeekday(DateTime dtInput)
        {
            return ((dtInput.DayOfWeek == DayOfWeek.Saturday) || (dtInput.DayOfWeek == DayOfWeek.Sunday)) ? false : true;
        }

If it's a weekday, it returns true, otherwise false.

Simple.Nice.Works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.