Michael Roskin
Michael Roskin

Reputation: 35

C# - Searching Time between 2 DateTime Objects of different days

i have a list of "event" objects.

In every event i have "EventStartTime" and "EventEndTime" declared as DateTime objects.

I want to be able to search "events" by time , for example 10:00, the "event" you see below shows that the festival starts at 22:00 on Feb 17th, and ends at 15:00 the following day. i have a couple more like these.

    new EventsManager.Event() //3
{
    EventType = EventsManager.EventType.Festival,
    EventName = "Twistival",
    EventPlace = placeList[4],
    EventStartTime =new DateTime(2017,02,17,22,0,0),
    EventEndTime = new DateTime(2017,02,18,15,0,0),
    EventNumberOfParticipants = 8000
},

So when i search for event that occur, or still occurring at at 10:00 i should get this event.

any suggestions?

Upvotes: 2

Views: 665

Answers (3)

juharr
juharr

Reputation: 32266

Assuming that you have a specific time of day that you want to determine if the event covers regardless of the date it covers it on then there are 4 cases you need to consider. First if the dates are more than 1 day apart they cover all times of day. If the start is before the time of day and the end is after the time of day it will cover the time. The last two cases require that the end date be on the next day from the start date, then either the start date is before the time of day, or the end date is after the time of day. Note that this also assumes that the start date is before the end date.

var events = new List<Tuple<DateTime, DateTime>>
{
    // start and end after time of day but on different days
    Tuple.Create(
        new DateTime(2017, 02, 17, 22, 0, 0), 
        new DateTime(2017, 02, 18, 15, 0, 0)),
    // start and end before time of day but on different days 
    Tuple.Create(
        new DateTime(2017, 02, 17, 9, 0, 0), 
        new DateTime(2017, 02, 18, 7, 0, 0)),
    // start before and end after same day 
    Tuple.Create(
        new DateTime(2017, 02, 17, 9, 0, 0), 
        new DateTime(2017, 02, 17, 11, 0, 0)),
    // covers more than 1 day
    Tuple.Create(
        new DateTime(2017, 02, 17, 22, 0, 0), 
        new DateTime(2017, 02, 18, 22, 0, 1)),
    // start after and end before on different days 
    Tuple.Create(
        new DateTime(2017, 02, 17, 22, 0, 0), 
        new DateTime(2017, 02, 18, 10, 0, 0)), 
    // start and end before on same day
    Tuple.Create(
        new DateTime(2017, 02, 17, 7, 0, 0), 
        new DateTime(2017, 02, 17, 8, 0, 0)), 
    // start and end after on same day
    Tuple.Create(
        new DateTime(2017, 02, 17, 11, 0, 0), 
        new DateTime(2017, 02, 17, 12, 0, 0)), 
};

var timeOfDay = new TimeSpan(0, 10, 0 ,0);

foreach (var x in events)
{
    if (x.Item2 - x.Item1 > TimeSpan.FromDays(1)
        || (x.Item1.TimeOfDay < timeOfDay && x.Item2.TimeOfDay > timeOfDay)
        || (x.Item1.Date < x.Item2.Date 
            && (x.Item1.TimeOfDay < timeOfDay || x.Item2.TimeOfDay > timeOfDay)))
    {
        Console.WriteLine(x);
    }

}

Will output

(2/17/2017 10:00:00 PM, 2/18/2017 3:00:00 PM)
(2/17/2017 9:00:00 AM, 2/18/2017 7:00:00 AM)
(2/17/2017 9:00:00 AM, 2/17/2017 11:00:00 AM)
(2/17/2017 10:00:00 PM, 2/18/2017 10:00:01 PM)

Upvotes: 1

Fruchtzwerg
Fruchtzwerg

Reputation: 11389

Let's say you have a

List<Event> Events;

of your Events. You can create a simple LINQ query to get all events running at a special time with a simple method like

private IEnumerable<Event> GetRunningEvents(DateTime time)
{
    return Events.Where(E => E.EventStartTime <= time && E.EventEndTime >= time);
}

Dont forget to add

using System.Linq;

to your file.

EDIT: Without LINQ a possible approach is

private List<Event> GetRunningEvents(DateTime time)
{
    List<Event> RunningEvents = new List<Event>();
    foreach(Event E in Events)
    {
        if (E.EventStartTime <= time && E.EventEndTime >= time)
        {
            RunningEvents.Add(E);
        }
    }
    return RunningEvents;
}

Upvotes: 1

Tito
Tito

Reputation: 365

Try Linq Where:

var list = new List<Event>();
var searchTime = DateTime.Now;
var result = list.Where(e => e.EventStartTime <= searchTime && searchTime <= e.EventEndTime).ToList();

Upvotes: 0

Related Questions