Formatting Date and Time Values In .NET

Friday Jan 24th 2003 by Bradley L. Jones

Dates and times can be formatted a number of different ways. Learn how to format dates and times in .NET. While this article uses C#, the lesson is applicable to VB .NET and Managed C++ programmers.

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:


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:


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:

Returns the date portion of a DateTime object
Returns the month portion of a DateTime object
Returns the day of the month of a DateTime object
Returns the year portion of the DateTime object
Returns the day of the week of a DateTime object
Returns the day of the year of a DateTime object
Returns the time portion of a DateTime object
Returns the hour portion of a DateTime object
Returns the minutes portion of a DateTime object
Returns the seconds portion of a DateTime object
Returns the milliseconds component of a DateTime object
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

SpecifierDescriptionDefault FormatExample Output
dShort datemm/dd/yyyy5/6/2001
DLong dateday, month dd, yyyySunday, May 06, 2001
fFull date/short timeday, month dd, yyyy hh:mm AM/PMSunday, May 06, 2001 12:30 PM
FFull date/full timeday, month dd, yyyy HH:mm:ss AM/PMSunday, May 06, 2001 12:30:54 PM
gShort date/short timemm/dd/yyyy HH:mm6/5/2001 12:30 PM
GShort date/long timemm/dd/yyyy hh:mm:ss6/5/2001 12:30:54 PM
M or mMonth daymonth ddMay 06
R or rRFC1123ddd, dd Month yyyy hh:mm:ss GMTSun, 06 May 2001 12:30:54 GMT
sSortable yyyy-mm-dd hh:mm:ss2001-05-06T12:30:54
tShort timehh:mm AM/PM12:30 PM
TLong timehh:mm:ss AM/PM12:30:54 PM
uSortable (universal) yyyy-mm-dd hh:mm:ss2001-05-06 12:30:54Z
USortable (universal) day, month dd, yyyy hh:mm:ss AM/PMSunday, May 06, 2001 12:30:54 PM
Y or yYear/monthmonth, yyyyMay, 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: 01/24/2002D: Friday, January 24, 2003f: Friday, January 24, 2003 1:06 PMF: Friday, January 24, 2003 1:06:51 PMg: 01/24/2003 1:06 PMG: 01/24/2003 1:06:51 PMm: January 24M: January 24r: Fri, 24 Jan 2003 13:06:51 GMTR: Fri, 24 Jan 2003 13:06:51 GMTs: 2003-01-24T13:06:51t: 1:06 PMT: 1:06:51 PMu: 2003-01-24 13:06:51ZU: Friday, January 24, 2003 01:06:51 PMy: 2003 JanuaryY: 2003 January

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 in .NET. 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 book, Sams Teach Yourself C# in 21 Days.

# # #

Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved