DateTime.Now
With this property 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.
Now changes when called the same way at different points in time. It has no side effects. For optimization, consider caching the result of DateTime.Now
.
When we assign to DateTime.Now
, the current time is copied to a storage location. This means that the time stored will never change after that point.
DateTime.Now
, the current time is computed. This is stored in a Structure
.DateTime
returned by Now is 10 seconds later. The value returned by Now keeps changing.Module Module1 Sub Main() ' Step 1: load now into local variable. Dim now As DateTime = DateTime.Now Console.WriteLine(now) ' Step 2: sleep 10 seconds. Threading.Thread.Sleep(10000) ' Step 3: use property again. Console.WriteLine(DateTime.Now) End Sub End Module6/23/2022 6:35:07 AM 6/23/2022 6:35:18 AM
Here we compare the result of DateTime.Now
with DateTime.Today
. The Today property is the same as Now, but with the time stripped.
DateTime
structure.Module Module1 Sub Main() Console.WriteLine("NOW: {0}", DateTime.Now) Console.WriteLine("TODAY: {0}", DateTime.Today) End Sub End ModuleNOW: 2/3/2020 4:33:38 AM TODAY: 2/3/2020 12:00:00 AM
DateTime.Now
can be stored in a local variable. The value of "Now" changes with time. Conceptually DateTime.Now
acts as a method that returns the current time.