2

I am having trouble comparing times.

From what I have researched it most likely is due to the time not having a date.

My code, This gets a dateTime value from the database.

var getDateTime = sql.Staff_Time_TBLs.Where(p => p.Staff_No ==
      SelectedEmployee.Key && p.Date_Data == day).Select(p => p.Time_Data_1).ToList();
DateTime dateTimeGet = Convert.ToDateTime(getDateTime);

dateTimeGet returns a value like this "2012/12/12 15:03:00.000"

I then declare variables to hold the time.

TimeSpan startCompare = TimeSpan.Parse("15:00");
TimeSpan endCompare = TimeSpan.Parse("21:00");

Then comparing the values Compare DateTime

if ((endCompare > dateTimeGet) && (startCompare < dateTimeGet))
     {
        //match found
     }

I am getting a compile error,

operands cannot be given to to type timespan and datetime

How do I compare times in this situation?

Community
  • 1
  • 1
KyloRen
  • 2,691
  • 5
  • 29
  • 59
  • [TimeSpan.Compare Method](https://msdn.microsoft.com/en-us/library/system.timespan.compare(v=vs.110).aspx) – Draken Jun 16 '16 at 11:19
  • 4
    Use `DateTime.TimeOfDay` perhaps? Then you'd be comparing two `TimeSpan` values... – Jon Skeet Jun 16 '16 at 11:20
  • 1
    @JonSkeet, that did the trick, thanks for that. Any idea why this post would be down voted? I did my best to find any of the answers before I posted here, as I am self taught thus far. I bought your book a couple months ago, still a bit advanced for me, but already found some great useful things even a person of my level can use. Thanks again. – KyloRen Jun 16 '16 at 11:34
  • @KyloRen its probably just because of the formatting, the way you've laid out your question is a little difficult to read – MikeT Jun 16 '16 at 12:17
  • @MikeT, sorry about that, did not know how else to put this. Thanks for the heads up. – KyloRen Jun 16 '16 at 12:42

4 Answers4

8

Just edit your code like this:

if ((endCompare > dateTimeGet.TimeOfDay) && (startCompare < dateTimeGet.TimeOfDay))
     {
        //match found
     }
lokusking
  • 7,396
  • 13
  • 38
  • 57
2

You could create DateTime values instead of TimeSpan to compare the value, using the Date of your db time:

DateTime startCompare = dateTimeGet.Date.AddHours(15);
DateTime endCompare = dateTimeGet.Date.AddHours(21);
if ((endCompare > dateTimeGet) && (startCompare < dateTimeGet))
{
    // match found
}

In the example you showed, actually would be enough to compare the Hour part of dateTimeGet:

if (dateTimeGet.Hour >= 15 && dateTimeGet.Hour <= 21)
    // match found
René Vogt
  • 43,056
  • 14
  • 77
  • 99
2

Actually you are comparing time with date in endCompare > dateTimeGet so you are getting the error

operands cannot be given to to type timespan and datetime

To compare time-span you need to extract the time from date in dateTimeGet by simply using TimeOfDay.

if ((endCompare > dateTimeGet.TimeOfDay) && (startCompare < dateTimeGet.TimeOfDay))
 {
    //match found
 }

This will convert the date into time. For more details about TimeOfDayclick here Hope this works fine for you.

Manish kumar
  • 109
  • 8
1

The issue is that, as you rightly say, you are comparing dates to times

A time-span is a measurement of time measured in Hours, where as a date-time is a measurement of time measured in days

so 2012/12/12 15:03:00.000 is approximately 735248.625 days or 17645967 hours which you are then comparing to a timespan of 15 hours

so you need to either add 735248 days to your time span or drop 735248 days form your Date

both can be easily done If you call the time TimeOfDay property on the date it will ignore the days and just return 0.625 days as 15 hours

Which means your code would look like this

if ((endCompare > dateTimeGet.TimeOfDay ) && (startCompare < dateTimeGet.TimeOfDay))

OR

If you add the time span to the at midnight date it will create the correct date time for comparation

Which means your code would look like this

if ((dateTimeGet.Date + endCompare > dateTimeGet ) && (dateTimeGet.Date + startCompare < dateTimeGet.TimeOfDay))

MikeT
  • 5,398
  • 3
  • 27
  • 43