New DateTime Techniques

Team Fly 

Page 38

In this example, the names of the animals can be used as keys to access the elements, instead of index numbers. Each key must be unique, though the data itself can be duplicated (''meat" and "meat" in this example).

New Date/Time Techniques

Before VB.NET, the Date function used to give you the current date (for example, 11/29/00). The Time function used to give you the current time. Now you must use the Today and TimeOfDay functions instead.

NOTE The old DATE$ and TIME$ functions have been eliminated.

In Visual Basic 6.0 and previous versions, a date/time was stored in a double (double-precision floating point) format (four bytes). In VB.NET, the date/time information uses the .NET Framework DateTime data type (stored in eight bytes). There is no implicit conversion between the Date and Double data types in VB.NET. To convert between the VB6 Date data type and the VB.NET Double data type, you must use the ToDouble and FromOADate methods of the DateTime class in the System namespace.

Here's an example that uses the TimeSpan object to calculate how much time elapsed between two DateTime objects:

 Dim StartTime, EndTime As DateTime Dim Span As TimeSpan         StartTime = "9:24" AM         EndTime = "10:14" AM         Span = New TimeSpan(EndTime.Ticks - StartTime.Ticks)         MsgBox(Span.ToString) 

Notice the Ticks unit of time. It represents a 100-nanosecond interval.

Here's another example illustrating the AddHours and AddMinutes methods, how to get the current time (Now), and a couple of other methods:

 Dim hr As Integer = 2 Dim mn As Integer = 13 Dim StartTime As New DateTime(DateTime.Now.Ticks) Dim EndTime As New DateTime(StartTime.AddHours(hr).Ticks) EndTime = EndTime.AddMinutes(mn)         Dim Difference = New TimeSpan(EndTime.Ticks - StartTime.Ticks)         Debug.WriteLine("Start Time is: " + StartTime.ToString("hh:mm"))         Debug.WriteLine("Ending Time is: " + EndTime.ToString("hh:mm")) 
Team Fly 


Visual Basic  .NET Power Tools
Visual Basic .NET Power Tools
ISBN: 0782142427
EAN: 2147483647
Year: 2003
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net