Add all dates between startdate and enddate to arraylist

M

Mike P

How do you take 2 dates (a startdate and an enddate), and then add all
the dates between them to an ArrayList?

This is as far as I have got so far, I'm not sure what to do next :

DateTime dtmStartDate = Convert.ToDateTime(dr["TaskStartDate"]);
DateTime dtmEndDate =
Convert.ToDateTime(dr["TaskEndDate"]);

TimeSpan ts = dtmEndDate - dtmStartDate;

arrTaskDates.Add(dtmStartDate);
 
J

Jon Skeet [C# MVP]

How do you take 2 dates (a startdate and an enddate), and then add all
the dates between them to an ArrayList?

This is as far as I have got so far, I'm not sure what to do next :

DateTime dtmStartDate = Convert.ToDateTime(dr["TaskStartDate"]);
DateTime dtmEndDate =
Convert.ToDateTime(dr["TaskEndDate"]);

TimeSpan ts = dtmEndDate - dtmStartDate;

arrTaskDates.Add(dtmStartDate);

If you look at my MiscUtil library (http://pobox.com/~skeet/csharp/
miscutil) you'll find a range class, and a way of iterating through
any particular range. Combined with extension methods, you could
write:

dtmStartDate.To(dtmEndDate).Step(1.Days()).ToList();

(Note that it would give you a List<T> rather than an ArrayList.)

I *think* the Step stuff uses Marc Gravell's generic operator code
which requires .NET 3.5 (in the version checked into MiscUtil
currently). What version of .NET are you using? If you're still on 1.1
(which is possible given your wish to use ArrayList) you'll need to
write a loop such as:

for (DateTime date = dtmStartDate; date <= dtmEndDate; date =
date.AddDays(1))
{
list.add(date);
}

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top