XTab's Blog

Ged Mead's Blog at vbCity

vbCity Blogs moved to:
http://cs.vbcity.com/blogs
  Home :: Syndication  :: Login

OctNovember 2009Dec
SMTWTFS
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

Archives

Topics

Ramblings

VB.NET

I've often thought that dealing with dates and times can sometimes be much more difficult than it should be. There is a bewildering range of choices, many of which seem to do pretty much the same thing and you can sometimes wonder which is the 'right' way. One good example of the multiple choices is identifying a time. I don't mean a TimeSpan - which is a period of time - but a single specific moment in time, such as 8 o'clock, that you want to isolate and maybe compare, manipulate or filter in some way.

You can of course take any DateTime instance and display only the time element by using ToShortTimeString or ToLongTimeString.

        TimeCheck = New DateTime

        TimeCheck = Now

        Console.WriteLine("TimeCheck =  " & TimeCheck.ToShortTimeString)

You can also drill down and pull out the hour, minute, second or any combination of those.

        TimeCheck = New DateTime

        TimeCheck = Now

        Console.WriteLine("TimeCheck Hour and Minute = {0}:{1}  ", _

           TimeCheck.Hour, TimeCheck.Minute)

Also available is the TimeOfDay property of the DateTime class, which - as you would expect - will return the time element. However this is a TimeSpan which in some cases can make it very fiddly to manipulate. It is also a ReadOnly property, so you can't use it to set a new time.

And by coincidence, only today I learned of yet another one - the TimeString property, which returns the current time in HH:mm:ss format.  But that is fixed to the current time, not some time point of your choice.

The TimeValue function offers a different approach. Although it gives the appearance of being just a time - e.g. 14:22:30 or 3:15 PM and so on - it is basically a full standard DateTime, but the date is always constrained to be 1st January in the year 0001. For most purposes when you use TimeValue it conveniently ignores that date, anyway.

You can extract the time value of any particular date and time by passing in a DateTime instance to the TimeValue function:

       Dim DT As DateTime = TimeValue(Now)

TimeValue returns a String, so there is no need to cast to any of the String formats to read it as text:

Console.WriteLine(DT)

If you run those last two lines of code, you may be surprised/disappointed to see that it still includes a date. However, remember that the original DateTime we passed in was the DateTime value Now. As you will see in the Console printout, the real value of today's date has been dropped and is replaced by the 01/01/0001 date.

TimeValue also allows you to manufacture a time. In the next example, a time of 1400 hours or 2pm is created:

        Dim TVTime As DateTime

        TVTime = TimeValue("14:00")

Note that unlike the overloaded constructors of the DateTime class, you don't have to include year, month and day - for the obvious reason that they would be ignored anyway.

Although I haven't done so, you can also include a value for Seconds to the time you create.

You can add or subtract elements of time from the currently stored time by using AddHours, AddMinutes, AddSeconds, etc

Finally, you can compare times. If you wanted, for example, to check if a particular deadline had passed you can compare the times extracted with TimeValue:

        Dim TVTime As DateTime

        TVTime = TimeValue("14:00")

        If TimeValue(Now) > TVTime Then

            Console.WriteLine("Passed deadline")

        Else

            Console.WriteLine("Not yet reached")

        End If

There is really nothing in TimeValue that can't be achieved by manipulating other properties and methods of the DateTime class, but in many situations it's a neat function that will make for easier, cleaner code.

posted on Saturday, September 05, 2009 11:18 AM

Feedback

# re: Date and Time: How To Create a 'Time' element 10/22/2009 12:10 AM fhmcfhm
fxhm xfh mhxfh mxfh fx

Post Feedback

Title:
Name:
Url:
Comments: 
Protected by Clearscreen.SharpHIPEnter the code you see: