Formatting Date and Time Values In .NET

Environment: .NET

The .NET Framework provide a class for working with dates and times—the DateTime class located in the System namespace. The DateTime class stores both a full date and the full time.

The DateTime class has a number of properties and methods that you will find useful. Additionally, there are a couple of static members. The two static properties that you will be likely to use are Now and Today. Now contains the date and time for the moment the call is made. Today returns the current date. You should note that although the Today property gives you only a valid date. It does not give you the current time, even though you can access time members.

Because these are static properties, their values can be obtained using the class name rather than an instant name. In other words, to get the current date or the current time, you need only do the following:

DateTime.Now

DateTime.Today

These commands assume that you have included the System namespace. If you didn’t include the namespace, then you’ll need to fully qualify the names:

System.DateTime.Now

System.DateTime.Today

You can review the online documents for information on all the methods and properties in DateTime. A few of the ones you might find useful are:

Date
Returns the date portion of a DateTime object
Month
Returns the month portion of a DateTime object
Day
Returns the day of the month of a DateTime object
Year
Returns the year portion of the DateTime object
DayOfWeek
Returns the day of the week of a DateTime object
DayOfYear
Returns the day of the year of a DateTime object
TimeOfDay
Returns the time portion of a DateTime object
Hour
Returns the hour portion of a DateTime object
Minute
Returns the minutes portion of a DateTime object
Second
Returns the seconds portion of a DateTime object
Millisecond  
Returns the milliseconds component of a DateTime object
Ticks
Returns a value equal to the number of 100-nanoseconds ticks for the given DateTime object

Formatting the Date and Time

When you work with strings and other output, you can use specifiers to indicate that formatting should occur. One of the most common times when specifiers are used is when using one of the System.Console classes, Write or WriteLine.

There are a number of specifiers that can be used specifically with dates, times, or both. These include the capability of displaying information in short and long format. Table 1 contains the date and time specifiers.

Table 1. Date And Time Formatting Characters

Specifier Description Default Format Example Output
d Short date mm/dd/yyyy 5/6/2001
D Long date day, month dd, yyyy Sunday, May 06, 2001
f Full date/short time day, month dd, yyyy hh:mm AM/PM Sunday, May 06, 2001 12:30 PM
F Full date/full time day, month dd, yyyy HH:mm:ss AM/PM Sunday, May 06, 2001 12:30:54 PM
g Short date/short time mm/dd/yyyy HH:mm 6/5/2001 12:30 PM
G Short date/long time mm/dd/yyyy hh:mm:ss 6/5/2001 12:30:54 PM
M or m Month day month dd May 06
R or r RFC1123 ddd, dd Month yyyy hh:mm:ss GMT Sun, 06 May 2001 12:30:54 GMT
s Sortable yyyy-mm-dd hh:mm:ss 2001-05-06T12:30:54
t Short time hh:mm AM/PM 12:30 PM
T Long time hh:mm:ss AM/PM 12:30:54 PM
u Sortable (universal) yyyy-mm-dd hh:mm:ss 2001-05-06 12:30:54Z
U Sortable (universal) day, month dd, yyyy hh:mm:ss AM/PM Sunday, May 06, 2001 12:30:54 PM
Y or y Year/month month, yyyy May, 2001
s is used as a specifier for printing a sortable date. Note that this is a lower case s. An uppercase S is not a valid format specifier and will generate an exception if used.

The date and time specifiers are easy to use. Listing 1 defines a simple date variable and then prints it in all the formats presented in Table 1.

Listing 1. dtformat.cs—The Date Formats

 1:  // dtformat.cs - date/time formats
 2:  //-----------------------------------------------
 3:
 4:  using System;
 5:
 6:  class myApp
 7:  {
 8:    public static void Main()
 9:    {
10:       DateTime CurrTime = DateTime.Now;
11:
12:       Console.WriteLine("d: {0:d}", CurrTime );
13:       Console.WriteLine("D: {0:D}", CurrTime );
14:       Console.WriteLine("f: {0:f}", CurrTime );
15:       Console.WriteLine("F: {0:F}", CurrTime );
16:       Console.WriteLine("g: {0:g}", CurrTime );
17:       Console.WriteLine("G: {0:G}", CurrTime );
18:       Console.WriteLine("m: {0:m}", CurrTime );
19:       Console.WriteLine("M: {0:M}", CurrTime );
20:       Console.WriteLine("r: {0:r}", CurrTime );
21:       Console.WriteLine("R: {0:R}", CurrTime );
22:       Console.WriteLine("s: {0:s}", CurrTime );
23:  //     Console.WriteLine("S: {0:S}", CurrTime );  // error!!!
24:       Console.WriteLine("t: {0:t}", CurrTime );
25:       Console.WriteLine("T: {0:T}", CurrTime );
26:       Console.WriteLine("u: {0:u}", CurrTime );
27:       Console.WriteLine("U: {0:U}", CurrTime );
28:       Console.WriteLine("y: {0:y}", CurrTime );
29:       Console.WriteLine("Y: {0:Y}", CurrTime );
30:    }
31:  }

The output from this listing is:

d: 5/6/2002
D: Sunday, May 06, 2002
f: Sunday, May 06, 2002 1:06 PM
F: Sunday, May 06, 2002 1:06:51 PM
g: 5/6/2002 1:06 PM
G: 5/6/2002 1:06:51 PM
m: May 06
M: May 06
r: Sun, 06 May 2002 13:06:51 GMT
R: Sun, 06 May 2002 13:06:51 GMT
s: 2002-05-06T13:06:51
t: 1:06 PM
T: 1:06:51 PM
u: 2002-05-06 13:06:51Z
U: Sunday, May 06, 2002 6:06:51 PM
y: May, 2002
Y: May, 2002

In line 10, this listing declares an object to hold the date and time. This is done using the DateTime class. This object is called CurrTime. It is assigned the static value from the DateTime class, Now, which provides the current date and time. Looking at the output, you can see that it was midday in May when I ran this listing. Lines 12 to 29 present this same date and time in all the date/time formats.

Line 23 is commented. This line uses the S specifier, which is not legal. If you uncomment this line, you will see that the listing throws an exception.

For More Information…

I’ll be posting a few additional aritcles on formatting. The next article will be on causing different formatting to occur based on whether a number is positive or negative. Additionally, you can get more information from my books, Sams Teach Yourself C# in 21 Days and Sams Teach Yourself the C# Language in 21 Days.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read