comparing just the time portion of time date variables

  • Thread starter Thread starter Paul
  • Start date Start date
P

Paul

Hi I am trying to compare just the time portion of two variables that are of
date time part. For example I have
dtdate1= 2/2/07 10:00:00
dtdate2=3/4/07 9:00:00
so for the comparision I would want 10:00:00 cmp with 9:00:00
so ddate1>ddate2 is true.
I tried converting to short time string but the comparison can not be done
on the strings.
Thanks Paul.
 
Hi I am trying to compare just the time portion of two variables that
are of
date time part. For example I have
dtdate1= 2/2/07 10:00:00
dtdate2=3/4/07 9:00:00
so for the comparision I would want 10:00:00 cmp with 9:00:00
so ddate1>ddate2 is true.
I tried converting to short time string but the comparison can not be
done on the strings.

Depending on the formatting you use, you should be able to compare them as
strings.

However, I think it would be simpler to just use the DateTime.TimeOfDay
property. Get the value of that property from each DateTime instance and
compare the two.

Pete
 
thanks for the response, will give that a try. The short time string worked
for == comparisons but not > or <. I ended up using additional variables and
just took the the short time string of both and copied the data into date
time variables which ends up using the current day for the missing day
portion of the variable. Sounds like the time of day will be easier and will
not have to use extra variables.
 
Hi,

IT can be as easy as getting the timespan from the start of the day:

TimeSpan ts1 = dtdate1.Substract( dtdate1.Today);
TimeSpan ts2 = dtdate2.Substract( dtdate1.Today);

Now you can compare them. as either numeric (Total(Minutes/second, etc) )
 
looks easy to impliment! Thanks for the additional information.
--
Paul G
Software engineer.


Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

IT can be as easy as getting the timespan from the start of the day:

TimeSpan ts1 = dtdate1.Substract( dtdate1.Today);
TimeSpan ts2 = dtdate2.Substract( dtdate1.Today);

Now you can compare them. as either numeric (Total(Minutes/second, etc) )
 
Um, or you could just really use the TimeOfDay property, like Peter
said:

TimeSpan ts1 = dtdate1.TimeOfDay;
TimeSpan ts2 = dtdate2.TimeOfDay;

And then compare those. No need to perform an extra operation when the
logic is encapsulated for you already.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Paul said:
looks easy to impliment! Thanks for the additional information.
 
Ended up just using the time span time of day as Pete suggested, got rid of
some extra variables as well!
--
Paul G
Software engineer.


Nicholas Paldino said:
Um, or you could just really use the TimeOfDay property, like Peter
said:

TimeSpan ts1 = dtdate1.TimeOfDay;
TimeSpan ts2 = dtdate2.TimeOfDay;

And then compare those. No need to perform an extra operation when the
logic is encapsulated for you already.
 
Back
Top