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# / May 2008

Tip: Looking for answers? Try searching our database.

DateTime.Compare question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
SandpointGuy - 28 May 2008 19:58 GMT
string dateTime = "5/28/2008";

DateTime.Compare(DateTime.Now,DateTime.Now) // returns 0
DateTime.Compare(DateTime.Now,DateTime.Parse(dateTime)) // returns 1

I need to compare 2 dates, using the second format, and I
dont understand why it isnt returning 0

Thank you
Jon Skeet [C# MVP] - 28 May 2008 20:17 GMT
> string dateTime = "5/28/2008";
>
[quoted text clipped - 3 lines]
> I need to compare 2 dates, using the second format, and I
> dont understand why it isnt returning 0

Because it's not midnight right now. If you only want to compare the
date components, use DateTime.Today.

Signature

Jon Skeet - <skeet@pobox.com>
Web site: http://www.pobox.com/~skeet   
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com

Ignacio Machin ( .NET/ C# MVP ) - 28 May 2008 21:44 GMT
On May 28, 2:58 pm, SandpointGuy
<Sandpoint...@discussions.microsoft.com> wrote:
> string dateTime = "5/28/2008";
>
[quoted text clipped - 5 lines]
>
> Thank you

Hi,
DateTme.Now include a time component.
Also it might be possible that the first line (using Now both times)
give also <>1, depending of how fast both evaluations are performed
Duggi - 29 May 2008 07:53 GMT
DateTime.Now inclides current time also. In the string you did not
give the time, means it takes as midnight 12:00. So its quite
meaningful to get 1 returned.

instead try the following

string dateTime = "5/29/2008";

DateTime.Compare(DateTime.Today, DateTime.Parse(dateTime)); //returns
0

Hope this should solve the issue that you are facing.

Thanks
Cnu

On May 28, 11:58 pm, SandpointGuy
<Sandpoint...@discussions.microsoft.com> wrote:
> string dateTime = "5/28/2008";
>
[quoted text clipped - 5 lines]
>
> Thank you

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.