In this post I have listed the different formatting options provided by .NET for a DateTime
object.
The examples shown below are based on the following DateTime
object:
// 18 September 2009 20:10:35 DateTime dt = new DateTime(2009, 09, 18, 20, 10, 35, 123);
Format specifier | Description | Result |
dt.ToString();
|
To String | 18/09/2009 20:10:35 |
dt.ToString("d"); dt.ToShortDateString();
|
Short date pattern. | 18/09/2009 |
dt.ToString("D"); dt.ToLongDateString();
|
Long date pattern. | 18 September 2009 |
dt.ToString("f");
|
Full date/time pattern (short time). | 18 September 2009 20:10 |
dt.ToString("F");
|
Full date/time pattern (long time). | 18 September 2009 20:10:35 |
dt.ToString("g");
|
General date/time pattern (short time). | 18/09/2009 20:10 |
dt.ToString("G");
|
General date/time pattern (long time). | 18/09/2009 20:10:35 |
dt.ToString("m"); dt.ToString("M");
|
Month/day pattern. | 18 September |
dt.ToString("o"); dt.ToString("O");
|
Round-trip date/time pattern. | 2009-09-18T20:10:35.1230000 |
dt.ToString("r"); dt.ToString("R");
|
RFC1123 pattern. | Fri, 18 Sep 2009 20:10:35 GMT |
dt.ToString("s");
|
Sortable date/time pattern. | 2009-09-18T20:10:35 |
dt.ToString("t"); dt.ToShortTimeString();
|
Short time pattern. | 20:10 |
dt.ToString("T"); dt.ToLongTimeString();
|
Long time pattern. | 20:10:35 |
dt.ToString("u");
|
Universal sortable date/time pattern. | 2009-09-18 20:10:35Z |
dt.ToString("U");
|
Universal full date/time pattern. | 18 September 2009 18:10:35 |
dt.ToString("y"); dt.ToString("Y");
|
Year month pattern. | September 2009 |
dt.ToBinary();
|
64-bit binary representation | 633889014351230000 |
dt.ToFileTime();
|
Windows file time | 128977710351230000 |
dt.ToFileTimeUtc();
|
Windows UTC file time | 128977782351230000 |
dt.ToLocalTime();
|
Local time representation | 18/09/2009 22:10:35 |
dt.ToOADate();
|
Equivalent OLE Automation date | 40074.840684294 |
dt.ToUniversalTime();
|
Coordinated Universal Time (UTC) | 18/09/2009 18:10:35 |
All the above examples which are using a format specifier character in the ToString()
method can also be written using the string.Format()
method. For example:
dt.ToString("U");
can be written as
string.Format("{0:U}", dt);
Although .NET supports a large number of predefined formats as shown above, it also allows you to create your own custom formats as we will see next.
The examples shown below are based on the following DateTime object:
// 03 September 2009 14:08:05 DateTime dt = new DateTime(2009, 09, 3, 14, 8, 5, 123);
Format specifier | Description | Result |
string.Format("{0:d}", dt);
|
The day of the month, from 1 through 31. | 3 |
string.Format("{0:dd}", dt);
|
The day of the month, from 1 through 31. | 03 |
string.Format("{0:ddd}", dt);
|
The abbreviated name of the day of the week. | Thu |
string.Format("{0:dddd}", dt);
|
The full name of the day of the week. | Thursday |
string.Format("{0:f}", dt);
|
The tenths of a second in a date and time value. | 1 |
string.Format("{0:ff}", dt);
|
The hundredths of a second in a date and time value. | 12 |
string.Format("{0:fff}", dt);
|
The milliseconds in a date and time value. | 123 |
string.Format("{0:ffff}", dt);
|
The ten thousandths of a second in a date and time value. | 1230 |
string.Format("{0:fffff}", dt);
|
The hundred thousandths of a second in a date and time value. | 12300 |
string.Format("{0:ffffff}", dt);
|
The millionths of a second in a date and time value. | 123000 |
string.Format("{0:fffffff}", dt);
|
The ten millionths of a second in a date and time value. | 1230000 |
string.Format("{0:F}", dt);
|
If non-zero, the tenths of a second in a date and time value. | 1 |
string.Format("{0:FF}", dt);
|
If non-zero, the hundredths of a second in a date and time value. | 12 |
string.Format("{0:FFF}", dt);
|
If non-zero, the milliseconds in a date and time value. | 123 |
string.Format("{0:FFFF}", dt);
|
If non-zero, the ten thousandths of a second in a date and time value. | 123 |
string.Format("{0:FFFFF}", dt);
|
If non-zero, the hundred thousandths of a second in a date and time value. | 123 |
string.Format("{0:FFFFFF}", dt);
|
If non-zero, the millionths of a second in a date and time value. | 123 |
string.Format("{0:FFFFFFF}", dt);
|
If non-zero, the ten millionths of a second in a date and time value. | 123 |
string.Format("{0:g}", dt); string.Format("{0:gg}", dt);
|
The period or era. | A.D. |
string.Format("{0:h}", dt);
|
The hour, using a 12-hour clock from 0 to 11. | 2 |
string.Format("{0:hh}", dt);
|
The hour, using a 12-hour clock from 00 to 11. | 02 |
string.Format("{0:H}", dt);
|
The hour, using a 24-hour clock from 0 to 23. | 14 |
string.Format("{0:HH}", dt);
|
The hour, using a 24-hour clock from 00 to 23. | 14 |
string.Format("{0:m}", dt);
|
The minute, from 0 through 59. | 8 |
string.Format("{0:mm}", dt);
|
The minute, from 00 through 59. | 08 |
string.Format("{0:M}", dt);
|
The month, from 1 through 12. | 9 |
string.Format("{0:MM}", dt);
|
The month, from 01 through 12. | 09 |
string.Format("{0:MMM}", dt);
|
The abbreviated name of the month. | Sep |
string.Format("{0:MMMM}", dt);
|
The full name of the month. | September |
string.Format("{0:s}", dt);
|
The second, from 0 through 59. | 5 |
string.Format("{0:ss}", dt);
|
The second, from 00 through 59. | 05 |
string.Format("{0:t}", dt);
|
The first character of the AM/PM designator. | P |
string.Format("{0:tt}", dt);
|
The AM/PM designator. | PM |
string.Format("{0:y}", dt);
|
The year, from 0 to 99. | 9 |
string.Format("{0:yy}", dt);
|
The year, from 00 to 99. | 09 |
string.Format("{0:yyy}", dt);
|
The year, with a minimum of three digits. | 2009 |
string.Format("{0:yyyy}", dt);
|
The year as a four-digit number. | 2009 |
string.Format("{0:yyyyy}", dt);
|
The year as a five-digit number. | 02009 |
string.Format("{0:z}", dt);
|
Hours offset from UTC, with no leading zeros. | +2 |
string.Format("{0:zz}", dt);
|
Hours offset from UTC, with a leading zero for a single-digit value. | +02 |
string.Format("{0:zzz}", dt);
|
Hours and minutes offset from UTC. | +02:00 |
Happy formatting. 🙂
Dave
there are 2 formats with the same specifier. Full date/time and tenths of a second use “f”.
In both examples with to String anf String.Format it outputs full date/time. So I don’t know which specifier to use to get tenths of second.
dt.ToString(“f”); Full date/time pattern (short time). 18 September 2009 20:10
string.Format(“{0:F}”, dt); If non-zero, the tenths of a second in a date and time value. 1