You want to get the current time in your C# program, and store it as a field or property in a class. Look into the DateTime.Now property and see how it is implemented, and how you can use it. Here we examine the DateTime.Now property in the System namespace, which you can use in any C# program.
First, the syntax used for DateTime.Now is that of 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. This example uses DateTime.Now, and stores it as a property in a class.
+++ Program that uses DateTime.Now (C#) +++
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 of the program +++
10/9/2009 9:45:06 PM
10/9/2009 9:45:06 PMUnderstanding the example. In the Main entry point, you can see the "now" variable being assigned to DateTime.Now. At this point, the "now" local variable contains the timestamp of the current DateTime.
Displaying the example. When you pass the "now" local variable to Console.WriteLine, it will be printed to the screen. There are many ways to format your DateTimes, but that is not in the scope of this article. [See "Formatting" below.]
Using DateTime in classes. The final lines of the example use the collection initializer syntax to set the HiringDate property to the DateTime.Now value. This means that the employee was just hired.
It is confusing and tricky to display dates in the exact way you want them in .NET. However, you can find some resources for this on this site. In this respect, DateTime.Now is simply a regular DateTime, with no special meaning.
(See DateTime Format Examples.)
When you assign a local variable to DateTime.Now, the internal value of DateTime.Now is copied. Your local variable will remain constant unless you reassign it. To illustrate this, the following example pauses (using the Thread.Sleep method) between assigning a local DateTime and then displaying it again. The values printed are the same.
(See Sleep Method Pauses Programs.)
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);
}
}Normally, properties in C# are retrieved based on a field that does not change and is fast to access. DateTime.Now is technically a property, but it is much slower and always changes. For these reasons, some experts regard DateTime.Now as a mistake. See CLR via C# (Second Edition) by Jeffrey Richter.
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 local variable and pass that as a parameter. This avoids the implementation.
Here we saw that DateTime.Now is a useful and convenient way to access the current time. When you assign your variable to DateTime.Now, the value is copied and will not update later. However, it is not fast like a normal property and is much more complicated.