Display calendar year with custom hyperlinks in asp.net mvc

I am looking to create an MVC webpage that displays 12 months of the year in a calendar format. Within each day of the month, I select only dates that have any activity (data from the database). Dates with activity will also be linked by a hyperlink to the route, e.g. / Activity / 2008/12/25

I'm going to try to use ajax toolkit calendar controls to control ajax, but wondered if anyone has any tips.

+3
source share
1 answer

Rendering a calendar is not extremely complicated. Using DateTimeFormatInfo in System.Globalization and DateTime, you can get all the necessary information:

  • DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek
  • DateTimeFormatInfo.CurrentInfo.GetMonthName()
  • DateTimeFormatInfo.CurrentInfo.GetAbbreviatedDayName((DayOfWeek) dayNumber)

:

_ _ _ 1 2 3 4 
5 6 7 8 9 ...

, - :

DateTime date = new DateTime(year, month, 1);
int emptyCells = ((int)date.DayOfWeek + 7 
    - (int)DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek) % 7;

31 6 , Ceil (37/7) = 6 . , 42 , .

, 7 ( ).

int days = DateTime.DaysInMonth(year, month);
for (int i = 0; i != 42; i++)
{
    if (i % 7 == 0) {
        writer.WriteLine("<tr>");
        if( i > 0 ) writer.WriteLine("</tr>");
    }

    if (i < emptyCells || i >= emptyCells + days) {
        writer.WriteLine("<td class=\"cal-empty\">&nbsp;</td>");
    } else {
        writer.WriteLine("<td class=\"cal-day\"\">" + date.Day + "</td>");
        date = date.AddDays(1);
    }
}

, , .

+6

Source: https://habr.com/ru/post/1706663/


All Articles