c# checking if Time in time interval

  • Thread starter Thread starter Sharon
  • Start date Start date
S

Sharon

Hi

I am reading string time values from text file example:

"08:09"

"8:44"



What is the correct way to handle (convent from string to Time) time objects
in c#?

And how do I check if the time object is in a specified range?



Example

If ( myTime > "9:00" && myTime < "11:00")



Thank you very much

Sharon.G
 
And how do I check if the time object is in a specified range?

Assuming you have converted your strings to DateTime objects already:

Dim Range1 as TimeSpan
Dim Range2 as TimeSpan
Range1 = EndTime - StartTime
Range2 = EndTime - TheTime
if(Range2 < Range1) then
MessageBox("TheTime is within the range!")
endif

Forgive me if my VB syntax isn't quite right, but you get the idea:
subtracting DateTime objects gives you a TimeSpans, which you can then
compare (or look at their "TotalMilliseconds" fields, or whatever).
 
Back
Top