checking time values
-
I have three time values and they are in string format. string time1 = "12:00:00" string time2 = "12:30:00" string time3 = "13:00:00" time2 changes, but time1 and time3 stay the same How can I check to see if time2 is between time1 and time3? I have thought of several different ways but they are all lengthy and involved. I though maybe something like this would work but want to make sure it will always work. if(time1 <= time2 <= time3) { do such -n- such } will this work for strings
-
I have three time values and they are in string format. string time1 = "12:00:00" string time2 = "12:30:00" string time3 = "13:00:00" time2 changes, but time1 and time3 stay the same How can I check to see if time2 is between time1 and time3? I have thought of several different ways but they are all lengthy and involved. I though maybe something like this would work but want to make sure it will always work. if(time1 <= time2 <= time3) { do such -n- such } will this work for strings
draco_iii wrote: if(time1 <= time2 <= time3) { The comparison of time1 <= time 2 returns a bool (if a comparable type, more on that shortly) which it would then compare that bool value to time3. It would be
time2 >= time1 && time2 <= time3
But there is another problem, you cannot use that as comparisons for strings. You would use the string.CompareTo() with returns a int value: < 0 : string less than target 0 : same > 0 : target less than string So you would have:
if( time2.CompareTo(time1) >= 0 && time2.CompareTo(time3) <= 0)
{
do...
}Rocky Moore <><