Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Toni Becker 146 posts 425 karma points
    Jul 28, 2011 @ 10:38
    Toni Becker
    0

    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.

    @{
    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?

    Thanks for every input

  • Jonas Eriksson 930 posts 1825 karma points
    Jul 28, 2011 @ 11:22
    Jonas Eriksson
    0

    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 :

    DateTimefirstDay =newDateTime(Model.yearToShow,Model.monthToShow,1); 
  • Toni Becker 146 posts 425 karma points
    Jul 28, 2011 @ 12:48
    Toni Becker
    0

    Changed and tested but giving the same error :/

  • Jonas Eriksson 930 posts 1825 karma points
    Jul 28, 2011 @ 12:58
    Jonas Eriksson
    0

    right - you need to cast to integers.

  • Toni Becker 146 posts 425 karma points
    Jul 28, 2011 @ 13:42
    Toni Becker
    0

    hmmm some tips how to write or better said rewrite :p

  • Toni Becker 146 posts 425 karma points
    Jul 28, 2011 @ 14:03
    Toni Becker
    0

    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>


Please Sign in or register to post replies

Write your reply to:

Draft