Time component of date time variable

  • Thread starter Thread starter Bob Day
  • Start date Start date
B

Bob Day

Using VS 2003...

How do you get the time component of a datetime variable? See below.

dim Date_And_Time as datetime = #2/5/04 4:00PM#

Dim Date_Component as datetime = Date_And_Time.Date

**** this does not work
Dim Time_Component as datetime = Date_And_Time.Time

I have resorted to complicated .ToShortTimeString, moving back and forth to
strings, but there must be a better way.

Please advise. THanks!

Bob
 
Bob,

You can use

------------
Dim Time_Component as Timespan= Date_And_Time.TimeOfDay
------------

Note that .TimeOfDay returns a timespan which represents the number of ticks
since 12am on the day in question. If you want to display the time
component, stick with the .ToShortTimeString method, but if you want to do
calculations on it, use the .TimeOfDay method instead.

Hope this helps,

Trev.
 
Bob Day said:
Using VS 2003...

How do you get the time component of a datetime variable? See
below.

dim Date_And_Time as datetime = #2/5/04 4:00PM#

Dim Date_Component as datetime = Date_And_Time.Date

**** this does not work
Dim Time_Component as datetime = Date_And_Time.Time

I have resorted to complicated .ToShortTimeString, moving back and
forth to strings, but there must be a better way.


Dim Time_Component As Timespan

Time_Component = Date_And_time.TimeOfday


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Bob,

* "Bob Day said:
How do you get the time component of a datetime variable? See below.

Do you want the time component to be stored in a 'DateTime'? I would
use the 'TimeSpam' returned by 'TimeOfDay', for example:

\\\
Dim d As Date = Date.Now
MsgBox(d.TimeOfDay.ToString())
///
 
Back
Top