If you set a Calendar control SelectedDate with a DateTime where the there is a date part, and a time of 00:00:00, the calendar correctly selects that date
If the time part is not midnight, the Calendar does not select the date.
I used the DateTime.Date value to set the date from a DateTime value with non-midnight dates
Is this a bug? or by quirky design
If it was easy, everybody would be doing it.
![]() |
0 |
![]() |
I could be wrong but more like quicky design because past midnight is in the 24hour clock which is in TimeSpan.
http://msdn2.microsoft.com/en-us/library/system.timespan.aspx
Kind regards,
Gift Peddie
![]() |
0 |
![]() |
The selected date property accepts the type of DateTime. It should therefore be able to extract the day/month/year out of this class and ignore the time part - which is how you and I would write the code! As I elucidated, the DateTime class already has a property that provides this, as well as other properties for day/month/year.
I am not sure why you think a time span is important. This is a different class supporting a different concept, and cannot be used to get a static date time
If it was easy, everybody would be doing it.
![]() |
0 |
![]() |
Yes I know they don't appear related but past midnight is multiple dates and I told you I could be wrong. Run a search on your problem you only find references to Oracle 10g docs why simple past midnight is time interval. I have seen you do time for users so you will find a solution but it is not a bug.
Kind regards,
Gift Peddie
![]() |
0 |
![]() |
Regardless of the calendar selection mode used, using a date time to select a single date with a time not equal to midnight does not perform a selection.
The time span is only relevant as when reading a range of dates already selected in the calendar, where the selected dates collection can be used to provide the first/last fixed position in time to create an abstract length of time (the timespan object)
A simple test
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void lnkDate_Click(object sender, EventArgs e)
{
DateTime dt = new DateTime(2006, 9, 21);
Calendar1.SelectedDate = dt;
Calendar1.VisibleDate = dt;
}
protected void lnkDateTime_Click(object sender, EventArgs e)
{
DateTime dt = new DateTime(2006, 9, 21, 12, 0, 0);
Calendar1.SelectedDate = dt;
Calendar1.VisibleDate = dt;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:LinkButton ID="lnkDate" runat="server" OnClick="lnkDate_Click">Set 21-Sep-2006</asp:LinkButton>
<asp:LinkButton ID="lnkDateTime" runat="server" OnClick="lnkDateTime_Click">Set 21-Sep-2006 12:00:00</asp:LinkButton>
<div>
<asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
</div>
</form>
</body>
</html>
If it was easy, everybody would be doing it.
![]() |
0 |
![]() |
Hi sbyard,
This is a very interesting find. I have reproduced your sample and can confirm if the "time" portion of the date is not midnight (12:00 AM) the SelectedDate will not be set.
I've been working with the Calendar control weekly for 5+ years and never ran into this problem before. It's really strange.
I'm going to do some digging and see if I can find where things are going wrong within the control. They (Microsoft) are probably doing a straight comparison of the SelectedDate value to the date of the Cell being rendered. If they don't match, the SelectedDate is not rendered. For example:
(DateTime.Today == DateTime.Now) // returns falseWhat I think should be happening is something like the following...
(DateTime.Today.ToShortDateString() == DateTime.Now.ToShortDateString()) // returns true
By removing the "Time" portion of the DateTime value, you can compare if the "days" are the same.Hope that makes sense.
I'll let you know what I find out.
Thanks,
Geoffrey McGill - Product Manager
Basic Date Picker - A Quicker Picker(TM) - ASP.NET Calendar, Date and Time Web Controls
![]() |
0 |
![]() |
Given that the DateTime class already has a property for the DAY part only, you would think that this would be used for teh comparison. But, as you allude, if there is a comparison between a DateTime and some other non-DateTime value, then a conversion is requried one way or the other. You can always use the standard calendar as a base class and coreect the issue by overriding the selected date property - as long as it's overridable, or even create a "new" property at the inherited level, and calling the base class.
Ho Hum
PS. If you see Derrick Zaychuck in Edmonton (has has a trucking company), say Hi. for me!
If it was easy, everybody would be doing it.
![]() |
0 |
![]() |