DateTime.Now (Current Time)
This page was last reviewed on Jun 22, 2022.
Dot Net Perls
DateTime.Now. This useful C# property returns the current time and day. The struct DateTime.Now returns can be stored as a field or property in a class.
More details. Here we look into this property and its implementation. And we determine the point at which DateTime.Now accesses the operating system for the current time.
First example. DateTime.Now is a static property. We do not call it on an instance of the DateTime struct. This code example uses DateTime.Now, and stores it as a property in a class.
Info We see the "now" variable being assigned to DateTime.Now. This variable contains the timestamp of the current DateTime.
Detail When you pass the "now" local to Console.WriteLine, it will be printed to the screen.
Tip The final lines of the example use the collection initializer syntax to set the HiringDate property to the DateTime.Now value.
using System; class Program { class Employee { public DateTime HiringDate { get; set; } } static void Main() { // Write the current date and time. // ... Access DateTime.Now. DateTime now = DateTime.Now; Console.WriteLine("NOW: " + now); // Store a DateTime in a class. // ... It no longer will be "now" as it is just a value in memory. Employee employee = new Employee() { HiringDate = now }; Console.WriteLine("HIRING DATE: " + employee.HiringDate); } }
NOW: 6/22/2022 8:09:18 AM HIRING DATE: 6/22/2022 8:09:18 AM
Copy. How are DateTime variables copied? When you assign a local variable to DateTime.Now, the internal value of DateTime.Now is copied.
Part 1 Here we copy the value of DateTime.Now into a local variable, and print its contents.
Part 2 We pause the program for 10 seconds using Thread.Sleep. This is to show that a local variable is not changed.
Part 3 Your local variable will remain constant unless you reassign it. Its value will not change in any other way.
using System; using System.Threading; class Program { static void Main() { // Part 1: copy value of DateTime.Now into local. DateTime now = DateTime.Now; Console.WriteLine(now); // Part 2: sleep for 10 seconds. Thread.Sleep(10000); // Part 3: the "now" local has not changed. // ... Its value is immutable. Console.WriteLine(now); } }
6/23/2020 6:30:43 AM 6/23/2020 6:30:43 AM
Format. It is hard to display dates in the exact way you want them. In this respect, DateTime.Now is simply a regular DateTime, with no special meaning.
DateTime Format
Internals. Normally, properties in C# are retrieved based on a field that does not change and is fast to access. DateTime.Now is a property, but it is much slower and always changes.
Detail Some experts regard DateTime.Now as a mistake. Please see the book CLR via C# (Second Edition) by Jeffrey Richter.
Performance. If you are working on a high-performance application, and need to use DateTime.Now, do not call it multiple times. Instead, cache it in a variable and pass that as a parameter.
Today. DateTime.Today is just like Now except it does not have a time. Instead it just has the day. The time part is initialized to zero.
A summary. DateTime.Now accesses the current time. When you assign your variable to DateTime.Now, the value is copied and will not update later. But it is not fast like a normal property.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 22, 2022 (new example).
© 2007-2024 Sam Allen.