HomeSearch

VB.NET DateTime.Now Property

This VB.NET example page uses the DateTime.Now property to get the current time.
DateTime.Now is a VB.NET property. With it we 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.DateTime
Let us begin with this sample program. When you access DateTime.Now, the current time is computed. This is stored in a Structure. Then, when you assign a local variable to that, the current time is copied in a storage location.

And: This means that the time stored will never change after that point. To get the current time again, access DateTime.Now again.

VB.NET program that uses DateTime.Now Module Module1 Sub Main() ' Load now into local variable. Dim now As DateTime = DateTime.Now Console.WriteLine(now) Threading.Thread.Sleep(10000) ' Use property again. Console.WriteLine(DateTime.Now) End Sub End Module Output 5/30/2011 7:00:40 AM 5/30/2011 7:00:50 AM
We see DateTime.Now returned a different value each time it was called. 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
Summary. 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.

And: It changes when called the 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.

© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls