Need a simple CRM and Project Management system?
Check out JobNimbus - CRM for Contractors and Service Professionals.

How to Highlight the Day in the ASP.NET Calendar Control with the SelectedDate Property

When you add an ASP.NET Calendar control to a page that might support edit capabilities, you may want to show the existing value in the Calendar control on post-backs. If you look into this, you'll probably notice the SelectedDate property of the control. This sets the date that is is selected in the control. So doing something like this, you might expect that the control would show the day highlighted automatically:

// set the selected date of the calendar control
Calendar1.SelectedDate = DateTime.Now;

 
But when you run this, you will see that no day is selected like you see here:

calendar with no day selected



  If you were to change DateTime.Now to DateTime.Today, it would highlight the correct day. So why?

There are a couple of problems here. First, the DateTime object is exactly that, a Date and a Time. So the Date, it would show 02/20/2009 and the Time is whatever time it is. Right now it is 3:44 PM. So when you use DateTime.Now, it is pulling the current time.

The Calendar control requires the EXACT date of the day you want to show. Meaning that in order to highlight the right day, you have to use 12:00:00 AM for the time. That's why DateTime.Today works. Because it returns the date and the exact time the day started which is 12:00:00 AM.

There is one more thing that you need to do. If you want to show a date that is not in the current month, the Calendar control will not automatically switch to that date. You have to use the VisibleDate property which tells the control what day to make visible. So the final code to set a date correctly would look like this:

// set the selected date of the calendar control
DateTime dateToSet = DateTime.Parse("02/02/2009 12:00:00 AM");
Calendar1.SelectedDate = dateToSet;
Calendar1.VisibleDate = dateToSet;

 
Notice the 12:00:00 AM time was specified so that it will show the date. Now when you run this code, you will see the following:

selected calendar day

This will show the day no matter what Date you set the SelectedDate and VisibleDate to.

highlighting selected date

really very helpful.
thanks

Another 'trick'

Dim DefaultDate = Now.AddDays(90)
DefaultDate -= DefaultDate.TimeOfDay
calDateExpected.SelectedDate = DefaultDate
calDateExpected.VisibleDate = DefaultDate

Subtracting the time of day will bring just the date.

YES

Oh my gosh thank you! You are a life saver!

Why have I had to waste 2 hours looking for a solution to this, come on microsoft sort it out, at least be consistent with your controls.

Calendar date time

Thank you this was very helpful. The syntex and examples made it easy to understand and utilize.

Thanks for the tip! This

Thanks for the tip! This helped me out today.

The Date.Property of DateTime can be helpfull, too

Assuming you get some date from anywhere you can jsut say

calendarRouteDay.SelectedDate = myVariableContainingTheDateTime.Date;
calendarRouteDay.VisibleDate = myVariableContainingTheDateTime.Date;

Nice tip

And just "Today", I learned a new System.Datetime function.