Ronaldinho Learn Coding
Ronaldinho Learn Coding

Reputation: 13824

Get the list of all weeks in a year group by month

I am working on a webform app and I need to list all of the weeks in a year, with these conditions:

Here is the output I am trying to archive, for example: this how it should looks from beginning of 2015 (starts from Monday Jan 5, 2015) until today July 29, 2015. I only write in details for Jun and July but the rest should be the same.

July, 2015

Monday (July, 27) - Sunday (August, 2) <= (each of these is a link)
Monday (July, 20) - Sunday (July, 26)
Monday (July, 13) - Sunday (July, 19)  
Monday (July, 6) - Sunday (July, 12)

Jun, 2015

Monday (June, 29) - Sunday (July, 5)
Monday (June, 22) - Sunday (June, 28)
Monday (June, 15) - Sunday (June, 21) 
Monday (June, 8) - Sunday (June, 14)
Monday (June, 1) - Sunday (June, 7)    

May to Jan should be the same...

This is what I am having now:

DateTime counter = new DateTime(2015, 1, 5); // first week of 2015 start on Monday Jan 5 2015
        while (counter <= DateTime.Now)
        {
            DateTime startDate = counter;
            DateTime endDate = startDate.AddDays(6);
            Response.Write("<a href=\"/details.aspx?start=" + startDate.ToShortDateString() + "&end=" + endDate.ToShortDateString() + "\" target=\"_blank\">" + startDate.ToLongDateString() + " - " + endDate.ToLongDateString() + "<br>");
            counter = endDate.AddDays(1);
        }

It outputs this:

enter image description here

how do I fix my loop to reverse it so the newer date is on top and also group/separate the list by month

Upvotes: 0

Views: 1310

Answers (2)

Enrique Zavaleta
Enrique Zavaleta

Reputation: 2108

Here you have your own code, I just add a few variables, I think it is more readable

DateTime counter = new DateTime(2015, 1, 5); // first week of 2015 start on Monday Jan 5 2015
int currentMonth = counter.Month;
List<string> rows = new List<string>();
while (counter <= DateTime.Now)
{
    DateTime startDate = counter;
    DateTime endDate = startDate.AddDays(6);
    if (!startDate.Month.Equals(currentMonth))
    {   //When the month change it writes the name of the new month
        rows.Add("<br>" + startDate.AddMonths(-1).ToString("MMMM"));
        currentMonth++;
    }
    //I delete your '<br>' at the end to use it in the Join(), also you were missing the closing tag '</a>'
    rows.Add("<a href='/details.aspx?start=" + startDate.ToShortDateString() + "&end=" + endDate.ToShortDateString() + "' target='_blank'>" + startDate.ToLongDateString() + " - " + endDate.ToLongDateString() + "</a>");
    counter = endDate.AddDays(1);
}
rows.Reverse(); //Reverse the order of the array
Response.Write(string.Join("<br>", rows.ToArray())); // Join the array in one line to just call one time the ResponseWrite

Upvotes: 1

user1666620
user1666620

Reputation: 4808

You reverse the loop:

int delta = DayOfWeek.Monday - DateTime.Now.DayOfWeek; // finds the difference between today and the monday of this week
DateTime counter = DateTime.Now.AddDays(delta); // sets the counter the first day of this week.

int currentMonth = DateTime.Now.Month; // current month as integer.

while (counter >= new DateTime(2015, 1, 5))
{
    if (currentMonth != counter.Month)
    {
        response.Write("my dividing text");// put your html here
        currentMonth = counter.Month;
    }

    DateTime startDate = counter;
    DateTime endDate = startDate.AddDays(6);

    Response.Write("<a href=\"/details.aspx?start=" 
        + startDate.ToShortDateString() + "&end=" + endDate.ToShortDateString() 
        + "\" target=\"_blank\">" + startDate.ToLongDateString() + " - " 
        + endDate.ToLongDateString() + "<br>");

    counter = startDate.AddDays(-7);
 }

Upvotes: 1

Related Questions