Calendar day binding

  • Thread starter Thread starter patrick delifer
  • Start date Start date
P

patrick delifer

I am binding some data to a calendar control. Since I don't want to return
all the Data from the DB, I would like to return only the data for the
current view of the calendar. The calendar has always 42 cells.
I would like to know if there is a way to know which date is displayed in
the first cell of the calendar. Unfortuantely, there is no index in the
calendar. ( I could go with the SelectedDate.Today as a reference and then
subtract and add 42 days but i would prefer if there is onther solution to
get the exact dates disoplayed..
Thx
 
patrick delifer said:
current view of the calendar. The calendar has always 42 cells.
I would like to know if there is a way to know which date is displayed in
the first cell of the calendar.

I'm not certain which Calendar you're using, so this may not be applicable.

On the ASP.NET Calendar (System.Web.UI.WebControls) you can get
the "current month" off of the TodaysDate property. The first row should
contain the first day of that current month ( Month 1, Year ).

Then, the date of the first day of the first week depends on what Day
of the Week that is, i.e., February 1, 2005 was a Tuesday.

Therefore, in a culture where Sunday is the first Day of the Week (which
you can read off of the FirstDayOfWeek property of the afforementioned
ASP.NET Calendar; in particular it will be Monday in some cultures)
then you subtract one day (Monday = 0, Tuesday = 1, ...) from the
number of days in the month preceding the "current month" (31 days
hath January...).

Answer: Sunday, January 30, 2005.

In summary -- find out what Month you're in, go to the first Day in that
Month, calculate the Day Of the Week, and then subtract to reach the
Date of the First Day Of Week (the amount you subtract is determined
by what Day Of the Week the first Day in the Month is, and the number
you subtract from is determined by the number of Days in the preceding
Month).

If you Calendar control doesn't keep the first day of the month on the
first row (if it just scrolls through six weeks at a time) then this algorithm
won't work all the time because sometimes you'll encounter the whole
first week as being days from the previous Month.


Derek Harmon
 
Good enough. But this takes into account the TodaysDate. I would have to
keep track of the todays date for other events (When the user selects a
previous or next month). Luckily, the VisibleMonthChanged of the calendar
control has a NewDate property which gets the date of first day of the
month.

I'll experiment with your suggestion. Thanks
 
Back
Top