Reputation: 789
I have started poking around C# a little bit, and the first challenge I got was creating a type of calendar. Its rather easy to type 1, 2, 3, 4.. till'
you got the dates you need, but since that will then be fixed, it will be incorrect for some months. I have tried searching everywhere, but right now I can't find anything that would help me, because it either only gives me the amount of days in the month, printing a load of 30's, or I don't get it to work.
As of right now I have a for loop that seems to be working, looping from 1, to the end of the month. I also have a list, but inside that list I need to know how to print the days in order to achieve what I am going for.
<%
var days = DateTime.DaysInMonth(2015, 11);
for (int i = 1; i < days; i++)
{
Response.Write("<div class='week'>");
Response.Write("<ul>");
Response.Write("<li>");
Response.Write(days);
Response.Write("</li>");
Response.Write("</ul>");
Response.Write("</div>");
}
%>
This is what I have right now, but all it does is print 30 30 times, and while I have tried other things, this is the closest thing I have gotten to it this far.
Upvotes: 1
Views: 2818
Reputation: 172378
Using LINQ you can try like this:
Enumerable.Range(1, DateTime.DaysInMonth(year, month))
.Select(day => new DateTime(year, month, day)).ToList();
EDIT:
As commented above by Jon you have not used the variable i in your loop, so you need,
Response.Write(i);
instead of
Response.Write(days);
Upvotes: 1