Okay writing on an event script via razor and inserting it via macro.
First here's the code for calculating the days etc.
@{ DateTime firstDay = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1); int daysInCurrentMonth = DateTime.DaysInMonth(firstDay.Year, firstDay.Month); DateTime lastDay = new DateTime(Model.yearToShow, Model.monthToShow, daysInCurrentMonth); // Sunday casted to int gives 0 but that will not work for us, we need 7 to be able to calculate number of empty cells correctly int dayOfWeekFirst = ((int)firstDay.DayOfWeek > 0) ? (int)firstDay.DayOfWeek : 7; int dayOfWeekLast = ((int)lastDay.DayOfWeek > 0) ? (int)lastDay.DayOfWeek : 7; }
But throws this error:
Error loading Razor Script calendar.cshtml The best overloaded method match for 'System.DateTime.DateTime(long, System.DateTimeKind, bool)' has some invalid arguments
Checked this out in Visual Studio, everything is okay. Could it be that there is a problem with referencing or assembly?
@{ DateTime firstDay = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1); int daysInCurrentMonth = DateTime.DaysInMonth(firstDay.Year, firstDay.Month); int yearToShow = DateTime.Now.Year; int monthToShow = DateTime.Now.Month; DateTime lastDay = new DateTime(yearToShow, monthToShow, daysInCurrentMonth); int dayOfWeekFirst = ((int)firstDay.DayOfWeek > 0) ? (int)firstDay.DayOfWeek : 7; int dayOfWeekLast = ((int)lastDay.DayOfWeek > 0) ? (int)lastDay.DayOfWeek : 7; }
<table id="reservationCalendar"> <thead> <tr> <td> << </td> <td colspan="5"> @firstDay.Year </td> <td> >> </td> </tr> </thead> <tr> <td> Mon </td> <td> Tue </td> <td> Wed </td> <td> Thu </td> <td> Fri </td> <td> Sat </td> <td> Sun </td> </tr> <tr> <!-- filling up space of previous month --> @for (int a = 1; a < dayOfWeekFirst; a++) { @:<td></td> } <!-- filling up space of current month --> @for (int i = 1; i <= daysInCurrentMonth; i++) { DateTime renderedDay = new DateTime(firstDay.Year, firstDay.Month, i);
// if Sunday if (renderedDay.DayOfWeek == DayOfWeek.Sunday) { @:<td class="calendar-holiday">@i</td></tr><tr> } // if Saturday else if (renderedDay.DayOfWeek == DayOfWeek.Saturday) { @:<td class="calendar-holiday">@i</td> } // if normal day else { @:<td>@i</td> } } <!-- filling up space of next month --> @for (int a = 1; a <= 7-dayOfWeekLast; a++) { @:<td></td> } </tr> </table>
DateTime Options and Issues
Okay writing on an event script via razor and inserting it via macro.
First here's the code for calculating the days etc.
But throws this error:
Error loading Razor Script calendar.cshtml
The best overloaded method match for 'System.DateTime.DateTime(long, System.DateTimeKind, bool)' has some invalid arguments
Checked this out in Visual Studio, everything is okay. Could it be that there is a problem with referencing or assembly?
Thanks for every input
Hi,
you're mixing DateTime.Now and monthToShow, so might be more days than allowed in that particular month, shouldn't the first line be :
Changed and tested but giving the same error :/
right - you need to cast to integers.
hmmm some tips how to write or better said rewrite :p
Sorry for my stupidity, got it working now:
@using umbraco.MacroEngines
@using System.Web
@{
DateTime firstDay = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
int daysInCurrentMonth = DateTime.DaysInMonth(firstDay.Year, firstDay.Month);
int yearToShow = DateTime.Now.Year;
int monthToShow = DateTime.Now.Month;
DateTime lastDay = new DateTime(yearToShow, monthToShow, daysInCurrentMonth);
int dayOfWeekFirst = ((int)firstDay.DayOfWeek > 0) ? (int)firstDay.DayOfWeek : 7;
int dayOfWeekLast = ((int)lastDay.DayOfWeek > 0) ? (int)lastDay.DayOfWeek : 7;
}
<table id="reservationCalendar">
<thead>
<tr>
<td>
<<
</td>
<td colspan="5">
@firstDay.Year
</td>
<td>
>>
</td>
</tr>
</thead>
<tr>
<td>
Mon
</td>
<td>
Tue
</td>
<td>
Wed
</td>
<td>
Thu
</td>
<td>
Fri
</td>
<td>
Sat
</td>
<td>
Sun
</td>
</tr>
<tr>
<!-- filling up space of previous month -->
@for (int a = 1; a < dayOfWeekFirst; a++)
{
@:<td></td>
}
<!-- filling up space of current month -->
@for (int i = 1; i <= daysInCurrentMonth; i++)
{
DateTime renderedDay = new DateTime(firstDay.Year, firstDay.Month, i);
// if Sunday
if (renderedDay.DayOfWeek == DayOfWeek.Sunday)
{
@:<td class="calendar-holiday">@i</td></tr><tr>
}
// if Saturday
else if (renderedDay.DayOfWeek == DayOfWeek.Saturday)
{
@:<td class="calendar-holiday">@i</td>
}
// if normal day
else
{
@:<td>@i</td>
}
}
<!-- filling up space of next month -->
@for (int a = 1; a <= 7-dayOfWeekLast; a++)
{
@:<td></td>
}
</tr>
</table>
is working on a reply...