943,724 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 19515
  • C# RSS
Jan 18th, 2007
0

ToString to Display day of the week

Expand Post »
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:

[
C# Syntax (Toggle Plain Text)
  1. <script Language="c#" runat="server">
  2. void Page_Load()
  3. {
  4.  
  5. DateTime CurrentTime;
  6. DateTime MyTime;
  7. CurrentTime = DateTime.Now;
  8. MyTime = DayOfWeek.Now;
  9. message1.Text = "<br>The current time is: " + CurrentTime;
  10. message2.Text = "<br>The Day of the week is: "+ MyTime.DayOfWeek.ToString();
  11. }
  12.  
  13. </script>
  14. <html>
  15. <head><title>Assignment2</title></head>
  16. <body>
  17. <h1>Assignment 2</h1>
  18. Cassandra Jackson ECT 310</a>
  19. <asp:label id="message1" runat="server"/>
  20. <asp:label id="message2" runat="server"/>
  21.  
  22. </body>
  23.  
  24. </html>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
cassyjack is offline Offline
36 posts
since Jan 2007
Jan 19th, 2007
0

Re: ToString to Display day of the week

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
Last edited by TylerSBreton; Jan 19th, 2007 at 12:05 pm.
Reputation Points: 25
Solved Threads: 3
Junior Poster in Training
TylerSBreton is offline Offline
89 posts
since Oct 2006
Jan 19th, 2007
0

Re: ToString to Display day of the week

Thanks A Bunch It Worked.
Reputation Points: 10
Solved Threads: 0
Light Poster
cassyjack is offline Offline
36 posts
since Jan 2007
Jan 23rd, 2007
0

Re: ToString to Display day of the week

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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Aun Muhammad is offline Offline
8 posts
since Jan 2007
Jan 30th, 2007
0

Re: ToString to Display day of the week

Try this:
C# Syntax (Toggle Plain Text)
  1. public static bool IsWeekday(DateTime dtInput)
  2. {
  3. return ((dtInput.DayOfWeek == DayOfWeek.Saturday) || (dtInput.DayOfWeek == DayOfWeek.Sunday)) ? false : true;
  4. }

If it's a weekday, it returns true, otherwise false.
Last edited by RichardCh; Jan 30th, 2007 at 1:11 pm. Reason: added comment
Reputation Points: 10
Solved Threads: 1
Newbie Poster
RichardCh is offline Offline
1 posts
since Jan 2007
Jan 31st, 2007
0

Re: ToString to Display day of the week

Click to Expand / Collapse  Quote originally posted by RichardCh ...
Try this:
C# Syntax (Toggle Plain Text)
  1. public static bool IsWeekday(DateTime dtInput)
  2. {
  3. return ((dtInput.DayOfWeek == DayOfWeek.Saturday) || (dtInput.DayOfWeek == DayOfWeek.Sunday)) ? false : true;
  4. }

If it's a weekday, it returns true, otherwise false.
Simple.Nice.Works.
Reputation Points: 14
Solved Threads: 0
Light Poster
Maidomax is offline Offline
39 posts
since Dec 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Do you know NET 2.0 documentation tools?
Next Thread in C# Forum Timeline: C# and ID3 tags





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC