Reputation: 1
I have 2 DateTimePickers: startDate
and endDate
I need to some how loop through all the weeks between those two dates. So for example all the weeks between 02/01/2013 - 02/01/2014.
I basically want the loop to start at 02/01/2013 and increment 7 days on each iteration until it gets to the end date. I've been trying think of a way to achieve this but I can't find a solution.
Is it possible?
Solved:
for (DateTime i = dateStart.Value; i <= dateEnd.Value; i = i.AddDays(7))
{
// Do something on each iteration
}
Will update answer in 8 hours. Won't let me answer my own question.
Upvotes: 0
Views: 115
Reputation: 44971
Here is one way to approach this:
var startDate = new DateTime(2013, 2, 1);
var endDate = new DateTime(2014, 2, 1);
var totalDays = endDate.Subtract(startDate).TotalDays;
// Determine the total number of weeks.
var totalWeeks = totalDays / 7;
// Initialize the current date to the start date
var currDate = startDate;
// Process each of the weeks
for (var week = 0; week < totalWeeks; week++)
{
// Do something
// Get the next date
currDate = currDate.AddDays(7);
}
Upvotes: 1