Can't Compare Dates in DotNet ??

  • Thread starter Thread starter Tom
  • Start date Start date
T

Tom

It appears that you can't compare two dates in DotNet. You must use
ToString and compare the strings. Is that the only reliable way?

Try this:

Dim dteOne As Date = FileDateTime(Application.ExecutablePath)
Dim dteTwo As Date = FileDateTime(Application.ExecutablePath)
SaveSetting("Test", "Dates", "DateThree",
FileDateTime(Application.ExecutablePath))
Dim dteThree As Date = CDate(GetSetting("Test", "Dates",
"DateThree"))

Console.WriteLine(dteOne)
Console.WriteLine(dteTwo)
Console.WriteLine(dteThree)
If dteOne = dteTwo Then
Console.WriteLine("dteOne = dteTwo")
End If
If dteOne = dteThree Then
Console.WriteLine("dteOne = dteThree")
End If
If dteOne.CompareTo(dteThree) = 0 Then
Console.WriteLine("dteOne.CompareTo(dteThree) = 0")
End If
If dteOne.ToString = dteThree.ToString Then
Console.WriteLine("dteOne.ToString = dteThree.ToString")
End If

I get these results:

10/14/2004 2:28:20 PM
10/14/2004 2:28:20 PM
10/14/2004 2:28:20 PM
dteOne = dteTwo
dteOne.ToString = dteThree.ToString

Tom
 
It appears that you can't compare two dates in DotNet. You must use
ToString and compare the strings. Is that the only reliable way?

Try this:

Dim dteOne As Date = FileDateTime(Application.ExecutablePath)
Dim dteTwo As Date = FileDateTime(Application.ExecutablePath)
SaveSetting("Test", "Dates", "DateThree",
FileDateTime(Application.ExecutablePath))
Dim dteThree As Date = CDate(GetSetting("Test", "Dates",
"DateThree"))

Console.WriteLine(dteOne)
Console.WriteLine(dteTwo)
Console.WriteLine(dteThree)
If dteOne = dteTwo Then
Console.WriteLine("dteOne = dteTwo")
End If
If dteOne = dteThree Then
Console.WriteLine("dteOne = dteThree")
End If
If dteOne.CompareTo(dteThree) = 0 Then
Console.WriteLine("dteOne.CompareTo(dteThree) = 0")
End If
If dteOne.ToString = dteThree.ToString Then
Console.WriteLine("dteOne.ToString = dteThree.ToString")
End If

I get these results:

10/14/2004 2:28:20 PM
10/14/2004 2:28:20 PM
10/14/2004 2:28:20 PM
dteOne = dteTwo
dteOne.ToString = dteThree.ToString

Tom

Option Strict On
Option Explicit On

Imports System

Module Module1

Sub Main()
Dim dt1 As New Date(2004, 1, 21)
Dim dt2 As New Date(2003, 1, 21)

Console.WriteLine(dt1)
Console.WriteLine(dt2)
Select Case Date.Compare(dt1, dt2)
Case Is < 0
Console.WriteLine("dt1 < dt2")
Case 0
Console.WriteLine("dt1 = dt2")
Case Is > 0
Console.WriteLine("dt1 > dt2")
End Select
End Sub

End Module

Look at the DateTime.Compare method in the documentation (Date in VB.NET
maps to the runtimes DateTime type).
 
Tom,
Comparing dates works just fine, your " If dteOne = dteTwo Then" proves it!

What seems to be "broken" is SaveSetting with an implicit Cast followed by
GetSetting.

When you convert a DateTime to a string using ToString you loose the
subseconds. Hence when you do a CDate on that string you do not restore the
subseconds. Seeing as dteOne & dteTwo both have subseconds while dteThree
does not, dteThree cannot equal the other two, unless you were lucky enough
that dteOne occurred exactly on a second! :-)

Try the following:

Const format As String = "yyyy-MM-dd HH:mm:ss.fffffff zzz"

Console.WriteLine(dteOne.ToString(Format))
Console.WriteLine(dteTwo.ToString(Format))
Console.WriteLine(dteThree.ToString(Format))

The "fffffff" displays 7 digits of sub seconds...

Remember that DateTime values are accurate to 100th of a nanosecond. In
other words Datetime is measured in 100-nanosecond units called ticks. You
can use DateTime.Ticks to see this value.

Hope this helps
Jay
 
Storing the DateTime value as a string drops the milliseconds portion of the
DateTime value, and that truncated value is the one you're storing in the
Registry, thus the dates don't compare exactly when you recreate the
DateTime by reading the registry. If you store the DateTime in the Registry
using a custom format string, such as "MM/dd/yyyy hh:mm:ss:ffff tt", then
you can use the DateTime.ParseExact method to parse it back out again into a
DateTime and retain the milliseconds portion. At that point, the dates
compare properly.
 
Russell,
See my post 4 f are not enough, you need all 7 f!

Try the following a number of different times, you will find count varies
from a relatively small number to a number well over 100!

Const format As String = "MM/dd/yyyy hh:mm:ss:ffff tt"
Dim d1 As DateTime
Dim d2 As DateTime

Dim count As Integer
Do
d1 = DateTime.Now
Dim s As String = d1.ToString(format)
d2 = Date.ParseExact(s, format,
System.Globalization.CultureInfo.CurrentCulture)

Debug.WriteLine(d1 = d2, "d1 = d2")
Debug.WriteLine(d1.Subtract(d2), "d1 - d2")
count += 1
Loop Until d1 = d2
Debug.WriteLine(count, "count")

Hope this helps
Jay
 
Thanks for the discussion. I've got the idea now. I'm in the process of
getting used to DotNet after using VB6 for years.

Tom
 

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