How to determine the entire date range displayed by an ASP.NET calendar?

The ASP.NET calendar always displays 6 weekly dates in a 7x6 grid. My problem is that the first day of the target month does not necessarily appear on the first line ... in some cases, the entire first line displays dates from the previous month. In other cases, the entire last line displays dates from the next line. Is there a reliable way to query the calendar object to determine the 42-day range that will be displayed for a specific month / year?

For example, consider June 2008 and February 2009:

Please note that the first week contains ONLY dates from the previous month http://img371.imageshack.us/img371/2290/datesmq5.png

I assume that the calendar tries to avoid grouping all the dates of the “other month” at the top or bottom of the grid and therefore puts the first of the target month in the second row. I am looking for an easy way to determine what the displayed range is for June 2008, for example, May 25 - July 5.

+5
source share
5 answers

Looking at the public users opened using the ASP.NET calendar, I don’t think that this information is what you can only get from the calendar control.

You have several options as a "workaround" to this, although, although not nice .... but they will work.

  • You can manually calculate the values ​​of the first week
  • "day render" min/max.

, AFAIK

Edit

- . , Day Render, 42 , DayRender, . "" , , .

, , .., ..

+5

, . Calendar.VisibleDate:

public static DateTime GetFirstDateOfMonth(DateTime date)
{
    return new DateTime(date.Year, date.Month, 1);
}

public static DateTime GetFirstDisplayedDate(DateTime date)
{
    date = GetFirstDateOfMonth(date);
    return date.DayOfWeek == DayOfWeek.Sunday ? date.AddDays(-7) : date.AddDays((int)date.DayOfWeek * -1);
}

public static List<DateTime> GetDisplayedDates(DateTime date)
{
    date = GetFirstDisplayedDate(date);

    List<DateTime> dates = new List<DateTime>();
    for (int i = 0; i < 42; i++)
    {
        dates.Add(date.AddDays(i));
    }

    return dates;
}
+5

Mitchel, , . public bool m_FirstDay = false

day_render

if(m_FirstDay == false)
{
    DateTime firstDate;
    DateTime lastDate;

    firstDate = e.Day.Date;
    lastDate = firstDate.AddDays(41);

    m_FirstDay = true;
}

asp.net. .

+2

. , . , , , FirstDayOfWeeks.

Reflector, , :


    ...
    DateTime visibleDate = this.EffectiveVisibleDate();
    DateTime firstDay = this.FirstCalendarDay(visibleDate);
    ...

private System.Globalization.Calendar threadCalendar = 
    DateTimeFormatInfo.CurrentInfo.Calendar;

private DateTime EffectiveVisibleDate()
{
    DateTime visibleDate = this.VisibleDate;
    if (visibleDate.Equals(DateTime.MinValue))
    {
        visibleDate = this.TodaysDate;
    }
    if (this.IsMinSupportedYearMonth(visibleDate))
    {
        return this.minSupportedDate;
    }
    return this.threadCalendar.AddDays(visibleDate, 
        -(this.threadCalendar.GetDayOfMonth(visibleDate) - 1));
}


private DateTime FirstCalendarDay(DateTime visibleDate)
{
    DateTime date = visibleDate;
    if (this.IsMinSupportedYearMonth(date))
    {
        return date;
    }
    int num = ((int) 
       this.threadCalendar.GetDayOfWeek(date)) - this.NumericFirstDayOfWeek();
    if (num <= 0)
    {
        num += 7;
    }
    return this.threadCalendar.AddDays(date, -num);
}

private int NumericFirstDayOfWeek()
{
    if (this.FirstDayOfWeek != FirstDayOfWeek.Default)
    {
        return (int) this.FirstDayOfWeek;
    }
    return (int) DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek;
}

private bool IsMinSupportedYearMonth(DateTime date)
{
    return this.IsTheSameYearMonth(this.minSupportedDate, date);
}

private bool IsTheSameYearMonth(DateTime date1, DateTime date2)
{
    return (((this.threadCalendar.GetEra(date1) == 
                   this.threadCalendar.GetEra(date2)) &&
          (this.threadCalendar.GetYear(date1) == 
                   this.threadCalendar.GetYear(date2))) &&
          (this.threadCalendar.GetMonth(date1) ==
                   this.threadCalendar.GetMonth(date2)));
}

Unfortunately, functionality already exists, we just can't figure it out!

+2
source

All Articles