This is a migrated thread and some comments may be shown as answers.

[Solved] Can't set min date = 2011/08/10

2 Answers 56 Views
Calendar
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Grzegorz Żydek
Top achievements
Rank 1
Grzegorz Żydek asked on 15 Jun 2011, 12:52 PM
Hi,

I've got some problem with calendar.

If I try put into view that code:
@(Html.Telerik().Calendar().Name("Month3").MinDate(new DateTime(2011,8,10)))

I have got error:

Year, Month, and Day parameters describe an un-representable DateTime.


Any sugesstions?

Cheers, Grzesiek

2 Answers, 1 is accepted

Sort by
0
Grzegorz Żydek
Top achievements
Rank 1
answered on 16 Jun 2011, 06:15 PM
Hi,

I finally solved problem with calendar.

There is a bug in Calendar.cs in BuildMonthView function.
private IHtmlNode BuildMonthView(ICalendarHtmlBuilder renderer)
        {
            IHtmlNode monthTag = renderer.MonthTag();
 
            DateTime? focusedDate = this.DetermineFocusedDate();
            DateTime prevMonth = new DateTime(focusedDate.Value.Year, focusedDate.Value.Month, 1).AddDays(-1);

            int firstDayOfMonthView = DateTime.DaysInMonth(prevMonth.Year, prevMonth.Month) - ((int)(prevMonth).DayOfWeek) + (int)CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
 
            DateTime startDate = new DateTime(prevMonth.Year, prevMonth.Month, firstDayOfMonthView);
            for (int weekRow = 0; weekRow < 6; weekRow++)
            {
                IHtmlNode rowTag = renderer.RowTag();
 
                for (int day = 0; day < 7; day++)
                {
                    renderer.CellTag(startDate, Value, urlFormat, startDate.Month != focusedDate.Value.Month).AppendTo(rowTag);
                    startDate = startDate.AddDays(1);
                }
                monthTag.Children.Add(rowTag);
            }
            return monthTag;
        }

Problem occurs when you want to evaluate  'firstDayOfMonthView'
int firstDayOfMonthView = DateTime.DaysInMonth(prevMonth.Year, prevMonth.Month) - ((int)(prevMonth).DayOfWeek) + (int)CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;

Everythink is fine when the first day of week is set to Sunday, but if you change to Monday (like in my example) you can get wrong day number.

For instance:
MinDate = 2011-08-01
First day of week set in CultureInfo is Monday

prevMonth = 2011-07-31, Days in prevMonth = 31,  Day of week for prevMonth = (Sunday)0,  First Day Of Week = (Monday)1
int firstDayOfMonthView = 31 - 0 + 1 = 32;
And that is wrong!

I solved this problem that way:
var dayOfWeekOfPrevMonth = ((int)(prevMonth).DayOfWeek);
if (dayOfWeekOfPrevMonth == 0) dayOfWeekOfPrevMonth = 7;
int firstDayOfMonthView = DateTime.DaysInMonth(prevMonth.Year, prevMonth.Month) - dayOfWeekOfPrevMonth + (int)CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;


Cheers, Grzesiek
0
Georgi Krustev
Telerik team
answered on 17 Jun 2011, 06:44 AM
Hello Grzegorz,

This is a known issue, which I am glad to inform is already fixed. The fix will be included in the next official release of Telerik extensions for ASP.NET MVC. 

Regards,
Georgi Krustev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Calendar
Asked by
Grzegorz Żydek
Top achievements
Rank 1
Answers by
Grzegorz Żydek
Top achievements
Rank 1
Georgi Krustev
Telerik team
Share this question
or