471,338 Members | 1,019 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

Add all dates between startdate and enddate to arraylist

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);

*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #1
2 2307
"Mike P" <mi*******@gmail.comwrote:
How do you take 2 dates (a startdate and an enddate), and then add all the
dates between them to an ArrayList?
Perhaps with a loop.

for (DateTime dt = start; dt <= end; dt = dt.AddDays(1))
{
list.Add(dt);
}

Eq.
Jun 27 '08 #2
On Jun 5, 12:01 pm, Mike P <mike.p...@gmail.comwrote:
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<Trather 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
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by David Gray | last post: by
2 posts views Thread by Mike N. | last post: by
2 posts views Thread by Child | last post: by
1 post views Thread by coolnav | last post: by
3 posts views Thread by =?Utf-8?B?V2FubmFiZQ==?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.