Link to home
Start Free TrialLog in
Avatar of mustish1
mustish1

asked on

Calendar that spanned four years

ASP.NET C# MVC VISUAL STUDIO 2019
I was trying to create this calendar that spanned four years   in ASP.NET MVC (excluding the weekends) on the index page but I made an error. Can you please help me out?

5/15/2023 5/22/2023 (Text light blue, background yellow)
15 16 17 18 19 22 23 24 25 26 (Text light green, background orange)
M T W T F M T W T F (Text pink, background white)
User generated image
User generated image

@{

    ViewBag.Title = "Calendar";

}


<h2>Calendar</h2>


<table class="calendar">

    <tr>

        <th colspan="5">Dates with Text: Light Blue, Background: Yellow</th>

        <th colspan="10">Dates with Text: Light Green, Background: Orange</th>

        <th colspan="5">Dates with Text: Pink, Background: White</th>

    </tr>

    <tr>

        @for (int year = DateTime.Now.Year; year <= DateTime.Now.Year + 3; year++)

        {

            for (int month = 1; month <= 12; month++)

            {

                DateTime firstDayOfMonth = new DateTime(year, month, 1);

                DateTime lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1);


                if (firstDayOfMonth.DayOfWeek != DayOfWeek.Saturday && firstDayOfMonth.DayOfWeek != DayOfWeek.Sunday)

                {

                    <th colspan="@lastDayOfMonth.Day">@firstDayOfMonth.ToString("MMMM yyyy")</th>

                }

            }

        }

    </tr>

    <tr>

        @for (int year = DateTime.Now.Year; year <= DateTime.Now.Year + 3; year++)

        {

            for (int month = 1; month <= 12; month++)

            {

                DateTime firstDayOfMonth = new DateTime(year, month, 1);

                DateTime lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1);


                if (firstDayOfMonth.DayOfWeek != DayOfWeek.Saturday && firstDayOfMonth.DayOfWeek != DayOfWeek.Sunday)

                {

                    for (int day = 1; day <= lastDayOfMonth.Day; day++)

                    {

                        DateTime currentDate = new DateTime(year, month, day);


                        string textColor, backgroundColor;

                        if (currentDate.Equals(new DateTime(2023, 5, 15)) || currentDate.Equals(new DateTime(2023, 5, 22)))

                        {

                            textColor = "lightblue";

                            backgroundColor = "yellow";

                        }

                        else if (currentDate.DayOfWeek != DayOfWeek.Saturday && currentDate.DayOfWeek != DayOfWeek.Sunday)

                        {

                            textColor = "lightgreen";

                            backgroundColor = "orange";

                        }

                        else

                        {

                            textColor = "pink";

                            backgroundColor = "white";

                        }


                        <td style="color: @textColor; background-color: @backgroundColor;">@currentDate.Day</td>

                    }

                }

            }

        }

    </tr>

    <tr>

        @for (int year = DateTime.Now.Year; year <= DateTime.Now.Year + 3; year++)

        {

            for (int month = 1; month <= 12; month++)

            {

                DateTime firstDayOfMonth = new DateTime(year, month, 1);

                DateTime lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1);


                if (firstDayOfMonth.DayOfWeek != DayOfWeek.Saturday && firstDayOfMonth.DayOfWeek != DayOfWeek.Sunday)

                {

                    for (int day = 1; day <= lastDayOfMonth.Day; day++)

                }

            }

Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Is that the entire code block?  Seems incomplete.

If it is:
The error is correct.  The final for loop in the code you posted doesn't have the ending }

@for (int year = DateTime.Now.Year; year <= DateTime.Now.Year + 3; year++)
        {
            for (int month = 1; month <= 12; month++)
            {
                DateTime firstDayOfMonth = new DateTime(year, month, 1);
                DateTime lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1);

                if (firstDayOfMonth.DayOfWeek != DayOfWeek.Saturday && firstDayOfMonth.DayOfWeek != DayOfWeek.Sunday)
                {
                    for (int day = 1; day <= lastDayOfMonth.Day; day++)
                }
            }

Open in new window

Avatar of mustish1

ASKER

User generated image


User generated image


Seems to be a missing {} where the red 'error' indicator is on the innermost FOR loop inside the IF.

Then the IF statement is probably missing a }

So, you need to decide what code block goes with what and make sure the { and } match up.

User generated image

User generated image


ASKER CERTIFIED SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial