locked
how to loop next and previous 12 months RRS feed

  • Question

  • User-1753299103 posted

    I need to list the next and the previous 12 months of the year form the current month with razor C.

    Thanks

    Saturday, February 21, 2015 2:50 PM

Answers

  • User1853794821 posted
    	var today=DateTime.Now;
    	for (int i = 13; i >0; i--)
    			{
    				@String.Format("{0:MMM}",today.AddMonths(-i))<br />
    			}
    	@String.Format("{0:MMM} *THIS MONTH*", today)<br />
    	for (int i = 1; i < 13; i++)
    	{
    			    @String.Format("{0:MMM}",today.AddMonths(i))<br />
    			}
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 21, 2015 3:22 PM
  • User1713851783 posted

    In the following an example of the code (but rrrsr7205 had already made most of the work):

    @{
        var months = new List<SelectListItem>();
        var today=DateTime.Now;
    
        // Add previous 12 months
        for (int i = 12; i >0; i--)
        {
            months.Add(new SelectListItem {
                Value = String.Format("{0:yyyyMM}01",today.AddMonths(-i)),
                Text = String.Format("{0:MMM-yyyy}",today.AddMonths(-i))
            });
    	}
    
        // Add current month
        months.Add(new SelectListItem {
            Value = String.Format("{0:yyyyMM}01",today),
            Text = String.Format("{0:MMM-yyyy}",today),
            Selected = true
        });
    
        // Add next 12 months
        for (int i = 1; i < 13; i++)
        {
            months.Add(new SelectListItem {
                Value = String.Format("{0:yyyyMM}01",today.AddMonths(i)),
                Text = String.Format("{0:MMM-yyyy}",today.AddMonths(i))
            });
    	}
    
        // Test form response
        var value = "";
        if (IsPost){
            value = Request["monthsDate"];
        }
    }
    
    <!DOCTYPE html>
    
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title></title>
        </head>
        <body>
            <form method="post">
                @Html.DropDownList("monthsDate", months)
                <input type="submit" />
            </form>
            <p>selection: @value</p>
        </body>
    </html>
    

    For an in depth description of the Html.DropDownList helper look at HTML Helpers For Forms In Razor Web Pages.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 22, 2015 5:37 AM
  • User1713851783 posted

    The following implementation highlights the selected date:

    @{
        var months = new List<SelectListItem>();
        var selectedDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
        var startDate = selectedDate.AddMonths(-12);
    
        if (IsPost){
            var resp = Request["monthsDate"];
            selectedDate = new DateTime(Convert.ToInt16(resp.Substring(0,4)), 
                    Convert.ToInt16(resp.Substring(4,2)), 1);
        }
        
        // Add 25 months
        for (int i = 0; i < 25; i++)
        {
            var curDate = startDate.AddMonths(i);
            months.Add(new SelectListItem {
                Value = String.Format("{0:yyyyMM}01",curDate),
                Text = String.Format("{0:MMM-yyyy}",curDate),
                Selected = (selectedDate == curDate)
            });
        }
    }
    
    

    If I understand what you mean with "in its corresponding row", I think that there are some jQuery plugins to enhance the look of a dropdown list: try googling.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 23, 2015 3:49 AM

All replies

  • User1853794821 posted
    	var today=DateTime.Now;
    	for (int i = 13; i >0; i--)
    			{
    				@String.Format("{0:MMM}",today.AddMonths(-i))<br />
    			}
    	@String.Format("{0:MMM} *THIS MONTH*", today)<br />
    	for (int i = 1; i < 13; i++)
    	{
    			    @String.Format("{0:MMM}",today.AddMonths(i))<br />
    			}
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 21, 2015 3:22 PM
  • User-1753299103 posted

    Thank you very much!

    Saturday, February 21, 2015 3:41 PM
  • User-1753299103 posted

    Sorry, I guess I spoke too soon thinking that your code can preform the intended task. But, I intend to have a form <select>that would display the CURRENTDATE as selected containing the LAST 12 months and the future 12 months. So, If I select for example, 2 months from now, then I would have 14 previous months and 8 future months and so on.

        <select>
            <option>looping previous 12 months</option>
            <option selected>Current month</option>
            <option>looping future 12 months</option>
        </select>

    I hope I explained myself clearly.

    Regards

    Saturday, February 21, 2015 11:26 PM
  • User1713851783 posted

    In the following an example of the code (but rrrsr7205 had already made most of the work):

    @{
        var months = new List<SelectListItem>();
        var today=DateTime.Now;
    
        // Add previous 12 months
        for (int i = 12; i >0; i--)
        {
            months.Add(new SelectListItem {
                Value = String.Format("{0:yyyyMM}01",today.AddMonths(-i)),
                Text = String.Format("{0:MMM-yyyy}",today.AddMonths(-i))
            });
    	}
    
        // Add current month
        months.Add(new SelectListItem {
            Value = String.Format("{0:yyyyMM}01",today),
            Text = String.Format("{0:MMM-yyyy}",today),
            Selected = true
        });
    
        // Add next 12 months
        for (int i = 1; i < 13; i++)
        {
            months.Add(new SelectListItem {
                Value = String.Format("{0:yyyyMM}01",today.AddMonths(i)),
                Text = String.Format("{0:MMM-yyyy}",today.AddMonths(i))
            });
    	}
    
        // Test form response
        var value = "";
        if (IsPost){
            value = Request["monthsDate"];
        }
    }
    
    <!DOCTYPE html>
    
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title></title>
        </head>
        <body>
            <form method="post">
                @Html.DropDownList("monthsDate", months)
                <input type="submit" />
            </form>
            <p>selection: @value</p>
        </body>
    </html>
    

    For an in depth description of the Html.DropDownList helper look at HTML Helpers For Forms In Razor Web Pages.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 22, 2015 5:37 AM
  • User-1753299103 posted

    Thank you, this works!

    Is there a way that the selected item can be shown inside the selectbox as SELECTED and in its corresponding row?

    Regards

    Sunday, February 22, 2015 8:22 PM
  • User1713851783 posted

    The following implementation highlights the selected date:

    @{
        var months = new List<SelectListItem>();
        var selectedDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
        var startDate = selectedDate.AddMonths(-12);
    
        if (IsPost){
            var resp = Request["monthsDate"];
            selectedDate = new DateTime(Convert.ToInt16(resp.Substring(0,4)), 
                    Convert.ToInt16(resp.Substring(4,2)), 1);
        }
        
        // Add 25 months
        for (int i = 0; i < 25; i++)
        {
            var curDate = startDate.AddMonths(i);
            months.Add(new SelectListItem {
                Value = String.Format("{0:yyyyMM}01",curDate),
                Text = String.Format("{0:MMM-yyyy}",curDate),
                Selected = (selectedDate == curDate)
            });
        }
    }
    
    

    If I understand what you mean with "in its corresponding row", I think that there are some jQuery plugins to enhance the look of a dropdown list: try googling.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 23, 2015 3:49 AM