Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have two dates in format of dd/mm/yyyy or dd/MM/yyyy. I want to compare both date but when I am using convert.todatetime(), its not giving me proper solution. There is any one have any idea about this comparisons.

I have googled but not got proper solution. This question may be repeated.

Please reply soon

share|improve this question
4  
Again, you aren't asking for a code review so this question is not appropriate here (for more information, see the FAQ). It would be more appropriate on Stack Overflow, but you would need to include more details, like how exactly doesn't Convert.ToDateTime() work for you. – svick Dec 19 '12 at 13:23

closed as off topic by svick, ANeves, almaz, Jesse C. Slicer, seand Dec 19 '12 at 15:33

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

2 Answers

Instead of using Convert.ToDateTime(), try DateTime.ParseExact:

var date1 = DateTime.ParseExact("01/02/2012", "dd/mm/yyyy",
                                CultureInfo.InvariantCulture, DateTimeStyles.None);
var date2 = DateTime.ParseExact("01/02/2012", "mm/dd/yyyy", 
                                CultureInfo.InvariantCulture, DateTimeStyles.None);
share|improve this answer

A date object always contains both a date component and a time component. When you fetch a DateTime's value via a format you are actually getting a string. The underlying DateTime object - it's value - is not changed at all.

DateTime date1 = new DateTime(2012, 12, 25, 3, 30, 0);
DateTime date2 = new DateTime(2012, 12, 25, 8, 15, 0);

// the dates are the same, the times are different

if (date1.ToShortDateString() == date2.ToShortDateString()) {
   Console.WriteLine ("Date1 equals Date2");        // you should get this
} else {
  Console.WriteLine ("Date1 DOES NOT equal Date2");
}

if (date1 == date2) {
   Console.WriteLine ("Date1 equals Date2");
} else {
  Console.WriteLine ("Date1 DOES NOT equal Date2"); // you should get this
}

if (date1.Date == date2.Date) {
   Console.WriteLine ("Date1 equals Date2");    // you should get this
} else {
  Console.WriteLine ("Date1 DOES NOT equal Date2"); 
}
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.