Nil Pun
Nil Pun

Reputation: 17373

C# Compare Time between Two Time Intervals

Trying to compare a given Time between two times to see if it's within the those intervals. e.g. if given Time is 00:00 I need to find out if it falls between 21:00:00 to 7:00:00. Tried TimeSpan.Compare no lock and also used > or < for Time Part.

e.g. Given Intervals:

7:00:00 to 19:00:00
19:00:00 to 21:00:00
21:00:00 to 7:00:00

Times to compare:

00:00:00 and 01:00:00

Any help will be appreciated.

Updated Question:

Looks like the requirement is quiet vague. The requirement is basically to pass the Time (TimeSpan) and compare with two TimeSpan intervals to see if they fall in to the those interval.

e.g. Lets say employees get different allowances if they work on different time slots below:

Date Range: 2012-01-01 to 2012-31

19:00:00 to 21:00:00 ($10.00)
21:00:00 to 7:00:00 ($11.00)
7:00:00 to 19:00:00 ($12.00)

To calculate the hourly rate for an employee I need to check whether the employee has worked

  1. Between Date Range :2012-01-01 to 2012-31
  2. Between Time Range above.

And apply $ Rate accordingly.

Upvotes: 3

Views: 35936

Answers (6)

Priyank shah
Priyank shah

Reputation: 1

string dt=DateTime.Now.ToShortTimeString();
DateTime presenttime=Convert.ToDateTime(dt);

starttime = starttimepicker.ValueString;
DateTime dtime=Convert.ToDateTime(starttime);

if (dtime > presenttime)
{
   MessageBox.Show("Time cannot be greater than System Time. Please Try Again!", "Do not selecting future time", MessageBoxButton.OK);
            starttimepicker.Value = presenttime;
}

Upvotes: 0

Branko Dimitrijevic
Branko Dimitrijevic

Reputation: 52107

The following code...

static class DateTimeExt {

    public static bool TimeOfDayIsBetween(this DateTime t, DateTime start, DateTime end) {

        var time_of_day = t.TimeOfDay;
        var start_time_of_day = start.TimeOfDay;
        var end_time_of_day = end.TimeOfDay;

        if (start_time_of_day <= end_time_of_day)
            return start_time_of_day <= time_of_day && time_of_day <= end_time_of_day;

        return start_time_of_day <= time_of_day || time_of_day <= end_time_of_day;

    }

}

class Program {

    static void Test(DateTime t, DateTime start, DateTime end) {
        bool falls_within = t.TimeOfDayIsBetween(start, end);
        Console.WriteLine("{0} \t[{1},\t{2}]:\t{3}", t, start, end, falls_within);
    }

    static void Main(string[] args) {

        Test(new DateTime(2012, 1, 1, 0, 0, 0), new DateTime(2012, 1, 1, 7, 0, 0), new DateTime(2012, 1, 1, 19, 0, 0));
        Test(new DateTime(2012, 1, 1, 1, 0, 0), new DateTime(2012, 1, 1, 7, 0, 0), new DateTime(2012, 1, 1, 19, 0, 0));

        Test(new DateTime(2012, 1, 1, 0, 0, 0), new DateTime(2012, 1, 1, 19, 0, 0), new DateTime(2012, 1, 1, 21, 0, 0));
        Test(new DateTime(2012, 1, 1, 1, 0, 0), new DateTime(2012, 1, 1, 19, 0, 0), new DateTime(2012, 1, 1, 21, 0, 0));

        Test(new DateTime(2012, 1, 1, 0, 0, 0), new DateTime(2012, 1, 1, 21, 0, 0), new DateTime(2012, 1, 1, 7, 0, 0));
        Test(new DateTime(2012, 1, 1, 1, 0, 0), new DateTime(2012, 1, 1, 21, 0, 0), new DateTime(2012, 1, 1, 7, 0, 0));

        Test(new DateTime(2012, 05, 17, 00, 00, 00, 00), new DateTime(2012, 05, 17, 20, 00, 00), new DateTime(2012, 05, 18, 08, 00, 00));
        Test(new DateTime(2012, 05, 17, 09, 00, 00, 00), new DateTime(2012, 05, 17, 20, 00, 00), new DateTime(2012, 05, 18, 08, 00, 00));

        Test(new DateTime(2012, 1, 1, 0, 0, 0), new DateTime(2012, 1, 1, 0, 0, 0), new DateTime(2012, 1, 1, 0, 0, 0));

    }

}

...prints the following result:

1/1/2012 12:00:00 AM    [1/1/2012 7:00:00 AM,   1/1/2012 7:00:00 PM]:   False
1/1/2012 1:00:00 AM     [1/1/2012 7:00:00 AM,   1/1/2012 7:00:00 PM]:   False
1/1/2012 12:00:00 AM    [1/1/2012 7:00:00 PM,   1/1/2012 9:00:00 PM]:   False
1/1/2012 1:00:00 AM     [1/1/2012 7:00:00 PM,   1/1/2012 9:00:00 PM]:   False
1/1/2012 12:00:00 AM    [1/1/2012 9:00:00 PM,   1/1/2012 7:00:00 AM]:   True
1/1/2012 1:00:00 AM     [1/1/2012 9:00:00 PM,   1/1/2012 7:00:00 AM]:   True
5/17/2012 12:00:00 AM   [5/17/2012 8:00:00 PM,  5/18/2012 8:00:00 AM]:  True
5/17/2012 9:00:00 AM    [5/17/2012 8:00:00 PM,  5/18/2012 8:00:00 AM]:  False
1/1/2012 12:00:00 AM    [1/1/2012 12:00:00 AM,  1/1/2012 12:00:00 AM]:  True

Upvotes: 3

RJ Lohan
RJ Lohan

Reputation: 6527

You could write youself an extension method like;

public static class TimeExtensions
{
    public static bool IsBetween(this DateTime time, DateTime startTime, DateTime endTime)
    {
        if (time.TimeOfDay == startTime.TimeOfDay) return true;
        if (time.TimeOfDay == endTime.TimeOfDay) return true;

        if (startTime.TimeOfDay <= endTime.TimeOfDay)
            return (time.TimeOfDay >= startTime.TimeOfDay && time.TimeOfDay <= endTime.TimeOfDay);
        else
            return !(time.TimeOfDay >= endTime.TimeOfDay && time.TimeOfDay <= startTime.TimeOfDay);
    }
}

Upvotes: 11

Shailesh
Shailesh

Reputation: 1218

Not sure why timespan is not working for you.

I tried this sample in my POC application and it worked.

 DateTime t1 = DateTime.Now;
    DateTime t2 = DateTime.UtcNow;
    t1.TimeOfDay.CompareTo(t2.TimeOfDay);

Try this hope it will solve the problem.

Upvotes: 0

Ian
Ian

Reputation: 4909

var time1 = DateTime.Now.TimeOfDay;
var time2 = DateTime.Now.AddDays(1.3).TimeOfDay;
var diff = time2 - time1;

So this is just for example perposes to show that adding 1.3 days still gives the same time answer.

Upvotes: 0

David Brabant
David Brabant

Reputation: 43489

Or, if your needs go beyond that, use one of my favorites libraries.

Upvotes: 1

Related Questions