
Using the DateTime struct in the C# language causes problems for many developers using the language at some point. The type has many methods, and many instructions are hard to follow. Here we see ways you can use DateTimes in practical ways for your applications.
This program introduces the DateTime type in the C# language. It creates an instance of a DateTime struct for May 10, 2010. Then, it prints the exact time and date to the screen with Console.WriteLine.
--- Program that uses DateTime [C#] ---
using System;
class Program
{
static void Main()
{
DateTime val = new DateTime(2010, 5, 10);
Console.WriteLine(val);
}
}
--- Output of the program ---
5/10/2010 12:00:00 AMDetailed examples. These articles represent the core of the DateTime tutorials available here. The first article provides examples for general usage of the DateTime type; the second article gives tips for formatting DateTimes in certain ways.
It is important to know how to format DateTime instances when you want to store the DateTime in another representation, such as text. The first article here provides a thorough description of DateTime formatting in the .NET Framework.
How can you convert a string into its equivalent DateTime representation? You can use one of the many different parsing methods available for this purpose. These articles detail the Parse, ParseExact, TryParse, and TryParseExact methods.
See DateTime.Parse String Method.
See DateTime.ParseExact Usage.
See DateTime.TryParse Example.
See DateTime.TryParseExact Method.
If you are dealing with certain Microsoft formats, you may encounter dates stored in double format. You can convert these into actual DateTime instances by using FromOADate, as shown in the linked article.
See FromOADate and Excel Dates.
This collection of examples deal with using months and days with the DateTime type. You can access the Month property; get all of the days of the week; or store arrays of all months and days.
When you subtract dates, you can sometimes end up with a time period of negative length. In most usages, though, you can find the difference between a later date and an earlier date, which is the elapsed time.
See Days Elapsed From DateTime.
You can never have a null DateTime instance, because the DateTime is a value type, not a reference type. However, you can use nullable DateTime? instances; and you can also special-case the MinValue in your code—as these articles demonstrate.
See DateTime Null, Using DateTime.MinValue Default.
One of the most useful properties on the DateTime type is the Now property: this returns the current date and time on the system. This article demonstrates the usage of this important property; it is quite appropriate for you to look at right now.
See DateTime.Now for Getting Current Time.
What should you do if DateTime computations are slowing down your program? One solution is to cache DateTime instances: this approach is demonstrated in this linked article.
See DateTime Performance Class.
As with other types in the C# language and .NET Framework, you can use the DateTime type in a variety of program contexts. These articles demonstrate some of these contexts in detail.
With the Timer type, you can create a recurrent event based on an interval of time. The Timer type is really useful for monitoring long-running processes such as websites.