Dailycode.info

Short solution for short problems

ASP .Net Calendar: Set selected date by default on today

As the title tells you, I was trying to set the default selected day of a asp .Net calendar to today. It looked like this:

Calendar1.SelectedDate = DateTime.Now;

But when the page loaded, the selected date was not shown, it was set, but not shown. So I found this solution: You have to use DateTime.Today instead of DateTime.Now.

Calendar1.SelectedDate = DateTime.Today;

In the comments, DevToolShed explained the following about this:

The reason this works with Today instead of Now is because of the Time part of the DateTime. DateTime.Now will return the current date and the current time. DateTime.Today returns the current date and "12:00:00 AM" as the time. To show a date selected on the calendar, the time must be exactly: 12:00:00 AM. Any other time will correctly be put into the property but will not show. 

That's all folks.