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\"> </td>");
} else {
writer.WriteLine("<td class=\"cal-day\"\">" + date.Day + "</td>");
date = date.AddDays(1);
}
}
, , .