Split Date range into weeks

Hi Experts,

In my c# program I am accepting a start date and end date parameter and processing data based on a week.  For example if a user enters a start date of 12/28/2014 - 3/02/2015, I want a list of ranges as such:

12/28/2014 - 01/04/2015
01/04/2015 - 01/11/2015
01/11/2015 - 01/18/2015

My program expects a start date of Sunday and end date of the next Sunday.  (process data less than end date).   I am looking to put this list of date ranges into an array or dictionary so I can then loop through and process each week.

User can also just enter a specific week such as 08/02/2015 - 08/09/2015 where I would just add those dates to the list and loop through the list once.

Thanks.
axnst2Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
And what is your concrete problem?
AndyAinscowFreelance programmer / ConsultantCommented:
Take the start date.  
Add to storage list.
Add 7 days to make next start date.  If later than end date stop.
Append this 'new' start date to list and loop back to previous stage.
it_saigeDeveloperCommented:
Does this fit your requirements?
using System;
using System.Collections.Generic;
using System.Linq;

namespace EE_Q28703767
{
	class Program
	{
		static void Main(string[] args)
		{
			DateTime start = new DateTime(2015, 8, 1);
			DateTime end = new DateTime(2015, 8, 31);

			Console.WriteLine("There are {0} weeks in {1} to {2}", start.GetWeeks(end).Count(), start.ToShortDateString(), end.ToShortDateString());
			foreach (var week in start.GetWeeks(end))
				Console.WriteLine("Start of Week: {0}; End of Week: {1}", week[0], week[1]);

			Console.ReadLine();
		}
	}

	static class Extensions
	{
		public static bool IsNullOrWhitespace(this string source)
		{
			return !string.IsNullOrEmpty(source) || source.TrimEnd().Length == 0;
		}

		public static IEnumerable<DateTime[]> GetWeeks(this string start, string end)
		{
			DateTime tempStart;
			DateTime tempEnd;
			if (start.IsNullOrWhitespace())
				throw new ArgumentNullException("start", "The start parameter cannot be null or empty");

			if (end.IsNullOrWhitespace())
				throw new ArgumentNullException("end", "The end parameter cannot be null or empty");

			if (!DateTime.TryParse(start, out tempStart))
				throw new ArgumentException("Cannot parse the start parameter to a date time", "start");

			if (!DateTime.TryParse(end, out tempEnd))
				throw new ArgumentException("Cannot parse the end parameter to a date time", "end");

			return tempStart.GetWeeks(tempEnd);
		}

		public static IEnumerable<DateTime[]> GetWeeks(this string start, DateTime end)
		{
			DateTime tempStart;
			if (start.IsNullOrWhitespace())
				throw new ArgumentNullException("start", "The start parameter cannot be null or empty");

			if (!DateTime.TryParse(start, out tempStart))
				throw new ArgumentException("Cannot parse the start parameter to a date time", "start");

			return tempStart.GetWeeks(end);
		}

		public static IEnumerable<DateTime[]> GetWeeks(this DateTime start, string end)
		{
			DateTime tempEnd;
			if (end.IsNullOrWhitespace())
				throw new ArgumentNullException("end", "The end parameter cannot be null or empty");

			if (!DateTime.TryParse(end, out tempEnd))
				throw new ArgumentException("Cannot parse the end parameter to a date time", "end");

			return start.GetWeeks(tempEnd);
		}

		public static IEnumerable<DateTime[]> GetWeeks(this DateTime start, DateTime end)
		{
			return (from w in Enumerable.Range(0, (int)(end.AddDays(-(int)end.DayOfWeek).AddDays(7).Subtract(start.AddDays(-(int)start.DayOfWeek)).TotalDays / 7)) 
				   select new DateTime[] { start.AddDays(-(int)start.DayOfWeek).AddDays(w * 7), start.AddDays(-(int)start.DayOfWeek).AddDays((w * 7) + 7) });
		}
	}
}

Open in new window


Which produces the following output -Capture.JPG
-saige-
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

axnst2Author Commented:
My problem is I am having issues trying to split the range into weeks.  I just figured it out.  I was making it more difficult than it needed to be.

for (int i = 0; i <= dayRange.Days; i+= 7)
{
       startDate = Global.PayrollPeriodStartDate.AddDays(i);
       DateRange.Add(startDate, startDate.AddDays(7));
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
axnst2Author Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for axnst2's comment #a40919545

for the following reason:

I was able to solve on my own.
AndyAinscowFreelance programmer / ConsultantCommented:
>>I was able to solve on my own.
Isn't that what I suggested?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.