DateTime Array interval

I want to create a DateTime Array wich counts 998 steps down from in 15 minute intervals

wich should like the following

Assuming the time is 20:00

{; 19:45; 19:30; 19:15;...}

I've never worked with Arrays directly so i am seeking out for help here

Jon Skeet

I'd use LINQ for this, in combination with DateTime.AddMinutes. LINQ makes it very easy to generate a sequence, and then convert that sequence into an array:

// Important: only call DateTime.Now once, so that all the values are
// consistent.
// Note that this will be the system local time - in many, many cases
// it's better to use UtcNow.
var now = DateTime.Now;
var times = Enumerable.Range(0, 998)
                      .Select(index => now.AddMinutes(index * -15))

A non-LINQ approach would first create an array of the right size, and populate it:

// Same comments about DateTime.Now apply
var now = DateTime.Now;
var times = new DateTime[998];
for (int i = 0; i < times.Length; i++)
    times[i] = now.AddMinutes(i * -15);

I definitely prefer the LINQ version..


See more on this question at Stackoverflow