By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,864 Members | 1,321 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,864 IT Pros & Developers. It's quick & easy.

comparing just the time portion of time date variables

P: n/a
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.
--
Paul G
Software engineer.
Jan 7 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Mon, 07 Jan 2008 10:49:00 -0800, Paul <Pa**@discussions.microsoft.com
wrote:
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
Jan 7 '08 #2

P: n/a
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.
--
Paul G
Software engineer.
"Peter Duniho" wrote:
On Mon, 07 Jan 2008 10:49:00 -0800, Paul <Pa**@discussions.microsoft.com>
wrote:
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
Jan 7 '08 #3

P: n/a
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) )

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"Paul" <Pa**@discussions.microsoft.comwrote in message
news:D6**********************************@microsof t.com...
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.
--
Paul G
Software engineer.

Jan 7 '08 #4

P: n/a
looks easy to impliment! Thanks for the additional information.
--
Paul G
Software engineer.
"Ignacio Machin ( .NET/ C# MVP )" wrote:
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) )

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"Paul" <Pa**@discussions.microsoft.comwrote in message
news:D6**********************************@microsof t.com...
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.
--
Paul G
Software engineer.


Jan 7 '08 #5

P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Paul" <Pa**@discussions.microsoft.comwrote in message
news:99**********************************@microsof t.com...
looks easy to impliment! Thanks for the additional information.
--
Paul G
Software engineer.
"Ignacio Machin ( .NET/ C# MVP )" wrote:
>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,
tc) )

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"Paul" <Pa**@discussions.microsoft.comwrote in message
news:D6**********************************@microso ft.com...
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.
--
Paul G
Software engineer.



Jan 7 '08 #6

P: n/a
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 [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"Paul" <Pa**@discussions.microsoft.comwrote in message
news:99**********************************@microsof t.com...
looks easy to impliment! Thanks for the additional information.
--
Paul G
Software engineer.
"Ignacio Machin ( .NET/ C# MVP )" wrote:
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,
tc) )

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"Paul" <Pa**@discussions.microsoft.comwrote in message
news:D6**********************************@microsof t.com...
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.
--
Paul G
Software engineer.


Jan 7 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.