By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,649 Members | 1,768 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,649 IT Pros & Developers. It's quick & easy.

Can't Compare Dates in DotNet ??

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
In article <u3*************@TK2MSFTNGP12.phx.gbl>, Tom wrote:
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 Shelton [MVP]
Nov 21 '05 #2

P: n/a
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

"Tom" <no*****@nowhere.none> wrote in message
news:u3*************@TK2MSFTNGP12.phx.gbl...
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

Nov 21 '05 #3

P: n/a
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.
"Tom" <no*****@nowhere.none> wrote in message
news:u3*************@TK2MSFTNGP12.phx.gbl...
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

Nov 21 '05 #4

P: n/a
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

"Russell Jones" <ar**@nospam.northstate.net> wrote in message
news:O9**************@TK2MSFTNGP12.phx.gbl...
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.
"Tom" <no*****@nowhere.none> wrote in message
news:u3*************@TK2MSFTNGP12.phx.gbl...
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


Nov 21 '05 #5

P: n/a
Tom
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

Tom wrote:
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

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.