Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do i check if the date selected in Calendar1 is a Monday, Tuesday, Wednesday, Thursday, Friday, Saturday or a Sunday using if, else statement?

As I want to write something like that:

C#
if (//The selected date is a Monday)
        {
            Label6.Text = "Monday";
        }

        else if (//The selected date is a Tuesday)
        {
            Label6.Text = "Tuesday";
        }
.
.
.
.
Posted
Updated 25-Jul-23 10:31am

Actually, you wouldn't need to do the if then statement at all. You can simply do a format:
C#
Label6.Text = Calendar1.SelectedDate.ToString("dddd");

That would put the full day of the week in the control. Here are more date formats you could use:

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^]
 
Share this answer
 
v3
Comments
kellycx 20-Jul-12 11:46am    
Thank You, I find your solution useful. However, the output i get is always the actual day today. For example, today is Friday but when i click a date that is on Wednesday, the output shown is still Friday. So how do i match the day selected to my Label6?
Tim Corey 20-Jul-12 12:05pm    
I've tweaked my answer slightly, assuming you are using a standard calendar control named Calendar1. This should fix your issue.
kellycx 20-Jul-12 12:11pm    
Thank You! It worked! (:
RaisKazi 20-Jul-12 13:49pm    
My 5.
Don't even need to go to that trouble
var x = DateTime.Now;
if(x.DayOfWeek == DayOfWeek.Tuesday);

I am assuming that Calendar returns a DataTime value. I do not work in ASP.NET

This is the exact solution:

Label6.Text = Calendar1.SelectedDate.DayOfWeek.ToString();
 
Share this answer
 
v2
var oldDay = new DateTime(1986, 2, 19);
Console.WriteLine(oldDay.DayOfWeek.ToString());
 
Share this answer
 
Comments
Richard Deeming 26-Jul-23 4:12am    
As already clearly explained in solution 2, posted ELEVEN YEARS AGO.

An unexplained code-dump which duplicates an existing solution is NOT a reason to resurrect an ancient solved question. If anything, it will get you kicked off the site for plagiarism.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900