Question

I have an array of timespans that are in 15 minute intervals, e.g.:

00:00:00,

00:15:00,

00:30:00,

00:45:00,

01:00:00

etc.

I want to loop through and only show those which are of a 30 minute interval. I've tried using the .compare but this doesn't get exactly what I need.

For example:

var interval = TimeSpan.FromMinutes(30);
foreach(var t in MyArray)
{
    if(TimeSpan.Compare(inteval, t.Time) == 0)
        Do something
    }
}

This technically works, but would only match 00:30:00. I can add the interval to the loop and use something like:

var interval = new TimeSpan(t.Hour, 30, 0)

but that only gets 00:30:00, 01:30:00.

Is there as way to make the hour like a wildcard and get every 30 min?

The output I'd want is:

00:00:00

00:30:00

01:00:00

01:30:00

etc.

Était-ce utile?

La solution

What you want is a modulo operation, but since TimeSpans themselves don't define that, use TotalMinutes (or another property) to get a plain integer and then use modulo:

if ((int)t.TotalMinutes % 30 == 0)

Autres conseils

You can also filter your array for every second item .. then use the new array and print is sequentially:

var newArray = MyArray.Where((t, index) => index % 2 == 0).ToArray();

This gives you every second item in the list.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top