代码之家  ›  专栏  ›  技术社区  ›  jsonGPPD

C&Razor:ASP.NET中使用HTML表的动态日历

  •  0
  • jsonGPPD  · 技术社区  · 8 年前
  • 如何修复我的表体以像日历一样正确地显示表的布局。

  • 到目前为止,这些是我在Razor页面中的代码。

    var lastdayof月=月的第一天addmonths(1).adddays(-1); } <th>星期日</th> <th>星期二</th> <th>周四</th> </thead> @对于(var i=0;i<numberoof days+1;i++) <td>@i</td> </tr>

    
    
    

    了解如何指定月份的具体第一天是星期日、星期一、星期二、星期三、星期四还是星期六。

  • 到目前为止,这些都是我在剃刀页面上的代码。

    @{ // responsible for getting the first and last days of the month
        var getDate = DateTime.Now;
    
        var firstDayOfTheMonth = new DateTime(getDate.Year, getDate.Month, 1);
        var lastDayOfTheMonth = firstDayOfTheMonth.AddMonths(1).AddDays(-1);
    
        var numberOfDays = Convert.ToInt16(lastDayOfTheMonth.ToString("dd"));
    }
    
    // My HTML table
    
    <table border="1">
    <thead>
        <tr>
            <th>Sunday</th>
            <th>Monday</th>
            <th>Tuesday</th>
            <th>Wednesday</th>
            <th>Thursday</th>
            <th>Friday</th>
            <th>Saturday</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            @for (var i = 0; i < numberOfDays + 1; i++)
            {
                <td>@i</td>
            }
        </tr>
    </tbody>
    

    enter image description here

    提前谢谢。

    2 回复  |  直到 7 年前
        1
  •  1
  •   user3559349    8 年前

    DateTime startDate = firstDayOfTheMonth.AddDays(-(int)firstDayOfTheMonth.DayOfWeek);
    

    <table>
        <thead>
            .... // add day name headings
        </thead>
        <tbody>
            <tr>
                @for (int i = 0; i < 42; i++)
                {
                    DateTime date = startDate.AddDays(i);
                    if (i % 7 == 0 && i > 0)
                    {
                        @:</tr><tr> // start a new row every 7 days
                    }
                    <td>@date.Day</td>
                }
            </tr>
        </tbody>
    </table>
    

    if (startDate.Month == getDate.month)
    {
        <td class="current">@date.Day</td>
    }
    else
    {
        <td>@date.Day</td>
    }
    
        2
  •  1
  •   Rikin Patel    7 年前

    ASP.NET MVC示例:

    @{
        int currentMonth = DateTime.Now.Month;
        int currentYear = DateTime.Now.Year;
        DateTime firstDay = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
        int daysInCurrentMonth = DateTime.DaysInMonth(firstDay.Year, firstDay.Month);
        DateTime lastDay = new DateTime(currentYear, currentMonth, 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;
    }
    

    <tr align="center">
        <!-- 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 align="center">
            }
            // 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>
    

    https://forums.asp.net/t/1695201.aspx?Making+a+Calendar+table+

    推荐文章