Francisco Garcia
Francisco Garcia

Reputation: 1044

Pulling a set of Weeks based on an inputted date range in C#

I end up custom building a lot of web based reports for clients of the software we produce, and I'm relatively new to development, so I encountered an interesting issue that I wasn't sure how to handle. I've worked out the specifics, but I'm sure there are better ways of handling this, and also if anyone encounters this I wanted to be able to share the result of my work this morning. I'm eager to be a productive member of the community, but am also open to any feedback regarding my question/answer/approach etc.

The report is generated and the user is allowed to give a start and an end date, but throughout the generation of the report the date ranges can mean multiple things. In one sector of the report that date range inputted pulls months in the range, which was easy enough to pull from the DateTime string broken up into manageable chunks. In other areas the range is treated as such, and the data pulled is aggregated over the range. The challenge I encountered was a sector of the report where data is aggregated based on the week so that the presentation of the report shows each week (1/1/1901 - 1/7/1901) and the columns of pertinent data follow for all the weeks affected in a chosen date range.

Upvotes: 1

Views: 304

Answers (1)

Francisco Garcia
Francisco Garcia

Reputation: 1044

What I did to alleviate the issue was to take the given date, and from that work out the date of the 'first' day of the week using the built in DateTimeFormat.FirstDayOfWeek, then establish the date of the end of that week adding six days to the date, committing the dates as start and end to a datatable, and then adding one to push the date into the following week.

Also, to ensure the set has an ending, I compare the inputted end date with the calculated end date of that week, and eventually the subtraction of those dates will end in a negative number or zero which denotes the inputted end date falls in the week just committed to DataTable. I'm sure there's a better way to do this, but it works nonetheless. I hope this saves at least one person the 20 minutes it takes. Thanks!

String sBeginDate = "";//Given in input
String sEndDate = "";//Given in input
DateTime weekStartDate;
DateTime weekEndDate;
DataTable dtWeeksInRange = new DataTable();                
DataColumn dcWeekStart = new DataColumn("WeekStart");
DataColumn dcWeekEnd = new DataColumn("WeekEnd");        
dtWeeksInRange.Columns.Add(dcWeekStart);
dtWeeksInRange.Columns.Add(dcWeekEnd);
DayOfWeek firstday = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;        
     weekStartDate = Convert.ToDateTime(sBeginDate) ;

    do
    {            

      while (weekStartDate.DayOfWeek != firstday) 
           { 
              weekStartDate = weekStartDate.AddDays(-1); 
           }
        weekEndDate = weekStartDate.AddDays(6);
        dtWeeksInRange.Rows.Add(weekStartDate, weekEndDate);
        weekStartDate = weekEndDate.AddDays(1);            
    }
      while ((Convert.ToDateTime(sEndDate).Subtract(weekEndDate)).Days > 0);

foreach (DataRow dr in dtWeeksInRange.Rows)
    {
      //Outputting the set of dates to the page
        Response.Write(Convert.ToDateTime(dr["WeekStart"]).ToShortDateString() + 
        " - " + 
        Convert.ToDateTime(dr["WeekEnd"]).ToShortDateString() + 
        "<br />");
    }

Upvotes: 1

Related Questions