DateTime.Compare question

  • Thread starter Thread starter SandpointGuy
  • Start date Start date
S

SandpointGuy

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
 
SandpointGuy said:
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

Because it's not midnight right now. If you only want to compare the
date components, use DateTime.Today.
 
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

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
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top