VB.NET DateTime.Now PropertyThis VB.NET example page uses the DateTime.Now property to get the current time.
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
' Load now into local variable.
Dim now As DateTime = DateTime.Now
' Use property again.
5/30/2011 7:00:40 AM
5/30/2011 7:00:50 AM
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
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 firstname.lastname@example.org.