
You want to get the current time in your VB.NET program. With the DateTime.Now property, you can get a copy of a Structure that stores the current time. We demonstrate DateTime.Now and show how it changes when accessed at a later time.
This VB article shows how to use the DateTime.Now property to get the current time.
Let's begin with this sample program. When you access DateTime.Now, the current time is computed and this is stored in a Structure type. Then, when you assign a local variable to that, the current time is copied in a storage location. This means that the time stored will never change after that point. To get the current time again, access DateTime.Now again.
Program that uses DateTime.Now [VB.NET]
Module Module1
Sub Main()
' Load now into local variable.
Dim now As DateTime = DateTime.Now
Console.WriteLine(now)
Threading.Thread.Sleep(10000)
' The next now.
Console.WriteLine(DateTime.Now)
End Sub
End Module
Output
5/30/2011 7:00:40 AM
5/30/2011 7:00:50 AMTime elapsed. You can see that DateTime.Now returned a different value each time it was called in the above program. The second DateTime returned by Now is ten seconds after the first DateTime. This is because the Sleep call caused ten seconds to pass before the second call.
Sleep Method
We saw how the DateTime.Now property can be stored in a local Dim, and how its value changes throughout the life of a program. Conceptually, DateTime.Now acts as a method that returns the current time; it changes when called the exact same way at different points in time. It has no side effects, but it is faster to store a DateTime and reuse it rather than call Now over and over again.
VB.NET Tutorials