Use the DateTime class's standard formatting methods in C#
The ToString and string.Format methods let you use formatting characters to format a DateTime value but it's not easy to remember all of the characters you might want to use. To make using standard formats easier, the DateTime class provides several formatting methods that return a DateTime value in a particular format. Note that these are locale-aware methods so the result you get depends on the system's configuration.
This example builds a ListView that demonstrates the various formatting methods. It then uses code from the example Make a program generate HTML table code for ListView data and display it in a WebBrowser control in C# to convert the results into the following HTML table.
See also:
Method | Equivalent | Result |
---|---|---|
ToLongDateString | D | Wednesday, March 17, 2010 |
ToLongTimeString | T | 10:08:37 AM |
ToShortDateString | d | 3/17/2010 |
ToShortTimeString | t | 10:08 AM |
ToString | G | 3/17/2010 10:08:37 AM |
- Use the DateTime class's standard formatting methods in C#
- Use custom date and time formats in C#
- Use custom numeric formats in C#
- Use standard date and time formats in C#
- Use standard numeric formats in C#
- Use standard numeric formats in C# (earlier version)
Comments