Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

comparing just the time portion of time date variables

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Paul - 07 Jan 2008 18:49 GMT
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.
Signature

Paul G
Software engineer.

Peter Duniho - 07 Jan 2008 19:03 GMT
> Hi I am trying to compare just the time portion of two variables that  
> are of
[quoted text clipped - 5 lines]
> 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
Paul - 07 Jan 2008 19:20 GMT
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.
Signature

Paul G
Software engineer.

> > Hi I am trying to compare just the time portion of two variables that  
> > are of
[quoted text clipped - 14 lines]
>
> Pete
Ignacio Machin ( .NET/ C# MVP ) - 07 Jan 2008 20:16 GMT
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)  )

Signature

Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.

> Hi I am trying to compare just the time portion of two variables that are
> of
[quoted text clipped - 6 lines]
> on the strings.
> Thanks Paul.
Paul - 07 Jan 2008 20:28 GMT
looks easy to impliment! Thanks for the additional information.
Signature

Paul G
Software engineer.

> Hi,
>
[quoted text clipped - 15 lines]
> > on the strings.
> > Thanks Paul.
Nicholas Paldino [.NET/C# MVP] - 07 Jan 2008 21:21 GMT
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.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> looks easy to impliment! Thanks for the additional information.
>
[quoted text clipped - 20 lines]
>> > on the strings.
>> > Thanks Paul.
Paul - 07 Jan 2008 21:24 GMT
Ended up just using the time span time of day as Pete suggested, got rid of
some extra variables as well!
Signature

Paul G
Software engineer.

>     Um, or you could just really use the TimeOfDay property, like Peter
> said:
[quoted text clipped - 29 lines]
> >> > on the strings.
> >> > Thanks Paul.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.