We help IT Professionals succeed at work.
Get Started

Find an algorythm to calculate weeks of a List<Datetime>

MSFanboy
MSFanboy asked
on
374 Views
Last Modified: 2012-05-09
Hello,

I am trying to find something generic without any hacks or too many checks/if`s etc.. because my solution works 99% but there are too many "exceptions" so I try to start again and find something better/more clear code.

Task:

I have a List of Datetimes selected by a user from a startdate with datepicker and a enddate from datepicker. For example user selected 01.07.2010 as startdate and as enddate 22.07.2010.

So my List of Datetimes has this: 01.07..,02.07..,03.07.,04.07..........21.07.,22.07.2010

PSEUDO-Code to give you a start help :P and to give you another HINT:

The first week object and the last week object in the final List<Week> CAN always be different from the week objects within the first AND last week object in the final List<Week>.

The problem is find something generic when you can not compute the week object with a mutual algorythm you can apply for EVERY week.

Now have fun:

var weekList = new List<Week>();
for( int i = 0; i < dateList.Count; i++) // Please do not consider the CurrentCulture because my firstDayOfWeek is ALWAYS MONDAY
{
     if(1 == (int)dateList[i].DayOfWeek) // When datetime in the dateList is a MONDAY (1)
     {
            
      }
      else // No MONDAY
       {

       }
}

[b]Week.cs:[/b]
public int WeekID {get;set;}
public List<Day> Days {get;set;}

[b]Day.cs:[/b]
public Datetime MeetingDate {get;set;}

Open in new window



Basing on my above dateList I should get this  List of Weeks as result:

Week1.Days:
01.07.2010
02.07.2010
03.07.2010
04.07.2010
Week2.Days:
05.07.2010
06.07.2010
07.07.2010
08.07.2010
09.07.2010
10.07.2010
11.07.2010
Week3.Days:
12.07.2010
13.07.2010
14.07.2010
15.07.2010
16.07.2010
17.07.2010
18.07.2010
Week4.Days:
19.07.2010
20.07.2010
21.07.2010
22.07.2010

How would you do the algo?


Comment
Watch Question
Senior Developer
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 2 Answers and 37 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE