C#:DateTime

.NET Array Dictionary List String 2D Async DataTable Dates DateTime Enum File For Foreach Format IEnumerable If IndexOf Lambda LINQ Parse Path Process Property Regex Replace Row Sort Split Static StringBuilder Substring Switch Tuple

DateTime.Now returns the current time and day. The struct it returns can be stored as a field or property in a class. This property is useful.
More details. We look into this property and its implementation. We find out where DateTime.Now accesses the operating system for the current time.
First example. DateTime.Now is a static property. This means you do not need to call it on an instance of the DateTime struct. Also, you do not use it with parenthesis or parameters.

Here: This code example uses DateTime.Now, and stores it as a property in a class.

PropertyClass
Based on: .NET 4.6

C# program that uses DateTime.Now

using System;

class Program
{
    class Employee
    {
	public DateTime HiringDate { get; set; }
    }

    static void Main()
    {
	//
	// Write the current date and time.
	//
	DateTime now = DateTime.Now;
	Console.WriteLine(now);

	//
	// Store a DateTime in a class.
	//
	Employee employee = new Employee() { HiringDate = now };
	Console.WriteLine(employee.HiringDate);
    }
}

Output

10/9/2014 9:45:06 PM
10/9/2014 9:45:06 PM
Program notes. We see the "now" variable being assigned to DateTime.Now. At this point, the "now" local variable contains the timestamp of the current DateTime.

WriteLine: When you pass the "now" local to Console.WriteLine, it will be printed to the screen.

Console.WriteLine

Tip: The final lines of the example use the collection initializer syntax to set the HiringDate property to the DateTime.Now value.


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
Copy. How are DateTime variables copied? When you assign a local variable to DateTime.Now, the internal value of DateTime.Now is copied.

And: Your local variable will remain constant unless you reassign it. Its value will not change in any other way.

Here: We pause (using Thread.Sleep) between assigning a local DateTime and then displaying it again. The values printed are the same.

Sleep
C# program that assigns to DateTime.Now

using System;

class Program
{
    static void Main()
    {
	DateTime now = DateTime.Now; // <-- Value is copied into local
	Console.WriteLine(now);
	System.Threading.Thread.Sleep(10000);

	//
	// This variable has the same value as before.
	//
	Console.WriteLine(now);
    }
}

Output
    (Depends on when you run the program.)

2/25/2014 11:12:13 AM
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.

Therefore: Some experts regard DateTime.Now as a mistake. Please see the book CLR via C# (Second Edition) by Jeffrey Richter.

Microsoft Learning
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.DateTime Performance
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.DateTime.Today
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.