date confusion

  • Thread starter Thread starter djc
  • Start date Start date
D

djc

I am confused with all the options for date values... What should I be using
when I want to compare (=, >, <) two dates, not including the time.. for
example when inserting data in a database the data may have the full date
and time like '12/15/2004 12:31:23 AM'. How do I convert that into just
'12/15/2004'? In other words I need to test if the current date is before or
after a stored date value, but the time does not matter.

any input is appreciated.
 
djc said:
I am confused with all the options for date values... What should I be
using
when I want to compare (=, >, <) two dates, not including the time.. for
example when inserting data in a database the data may have the full date
and time like '12/15/2004 12:31:23 AM'. How do I convert that into just
'12/15/2004'? In other words I need to test if the current date is before
or
after a stored date value, but the time does not matter.

'If d1.Date = d2.Date Then...'.
 
djc,
Use DateTime.Date to get just the date part of a DateTime value.

Dim date1 As DateTime = #12/15/2004 12:31:23 AM#
Dim date2 As DateTime = #12/15/2004#

Debug.WriteLine(date1 = date2, "date1 = date2")
Debug.WriteLine(date1.Date = date2, "date1.Date = date2")

You can use DateTime.TimeOfDay to get just the time part of a DateTime
value.

Dim time1 As TimeSpan = #12:31:23 AM#.TimeOfDay

Debug.WriteLine(date1.TimeOfDay.Equals(time1), "date1 = time1")

Unfortunately the equal "=" operator is not overloaded to compare TimeSpan
values, we need to wait for VS.NET 2005 (aka Whidbey, due out later in 2005)
to allow overloading the equal operator.

' VS.NET 2005 syntax
Debug.WriteLine(date1.TimeOfDay = time1, "date1 = time1")

WARNING: Time values includes fractions of milliseconds, so be careful when
comparing them for equality (less or greater usually works better).

Hope this helps
Jay
 
Thanks for the reply!

Jay B. Harlow said:
djc,
Use DateTime.Date to get just the date part of a DateTime value.

Dim date1 As DateTime = #12/15/2004 12:31:23 AM#
Dim date2 As DateTime = #12/15/2004#

Debug.WriteLine(date1 = date2, "date1 = date2")
Debug.WriteLine(date1.Date = date2, "date1.Date = date2")

You can use DateTime.TimeOfDay to get just the time part of a DateTime
value.

Dim time1 As TimeSpan = #12:31:23 AM#.TimeOfDay

Debug.WriteLine(date1.TimeOfDay.Equals(time1), "date1 = time1")

Unfortunately the equal "=" operator is not overloaded to compare TimeSpan
values, we need to wait for VS.NET 2005 (aka Whidbey, due out later in 2005)
to allow overloading the equal operator.

' VS.NET 2005 syntax
Debug.WriteLine(date1.TimeOfDay = time1, "date1 = time1")

WARNING: Time values includes fractions of milliseconds, so be careful when
comparing them for equality (less or greater usually works better).

Hope this helps
Jay
 

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