Reputation: 483
I have 2 DateTimePicker
controls named dtp1
and dtp2
. I wish to get an array of dates between those 2 days: dtp1.Date.Value <= x <= dtp2.Date.Value.
I currently use for
loop to achieve such a task, but this is not a very efficient way to do things:
int c = (int)(dtp2.Value.Date-dtp1.Value.Date).TotalDays + 1;
DateTime[] d = new DateTime[c];
for (int i = 0; i < c; i++)
{
d[i] = dtp1.Value.Date.AddDays(i);
}
Is there any better and concise way to achieve the same result?
Upvotes: 1
Views: 2284
Reputation: 236
I advise you to use lists instead arrays and u can use Enumarable.Range
var startDate = new DateTime(2013, 1, 25);
var endDate = new DateTime(2013, 1, 31);
int days = (endDate - startDate).Days + 1; // incl. endDate
List<DateTime> range = Enumerable.Range(0, days)
.Select(i => startDate.AddDays(i))
.ToList();
You can learn much more about Lists here
Upvotes: 4