954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

ToString to Display day of the week

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 = "The current time is:  " + CurrentTime;
    message2.Text = "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>
cassyjack
Light Poster
36 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

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

TylerSBreton
Junior Poster in Training
89 posts since Oct 2006
Reputation Points: 25
Solved Threads: 3
 

Thanks A Bunch It Worked.

cassyjack
Light Poster
36 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

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

Aun Muhammad
Newbie Poster
8 posts since Jan 2007
Reputation Points: 10
Solved Threads: 1
 

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.

RichardCh
Newbie Poster
1 post since Jan 2007
Reputation Points: 10
Solved Threads: 1
 

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.

Maidomax
Light Poster
39 posts since Dec 2006
Reputation Points: 14
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You