jump to navigation

VB.NET and DateTime Comparison March 09 2006

 After doing some DateTime comparison tests, I have come to some strange conclusions.

 The DateTime.Compare() method is not reliable when comparing a database record DateTime with a VB.NET DateTime

 The “=” comparison is not reliable when comparing a database record DateTime with a VB.NET DateTime

The easiest way to do the datetime comparison between various forms of the DateTime datatype is to simply convert it to a string and compare!

 e.g.

Dim dr as DataRow

Dim dt1 as DateTime

Dim dt2 as DateTime

… Fill ‘dr’ with some database data…

dt1 = Now()

dt2 = dr.item(”some_db_datetime”)

if(dt1.toString.trim = dt2.toString.trim) then

  ‘ True Condition

else

   ‘ False Condition

EndIf

 

Comments»

no comments yet - be the first?