Use the DateTime class’s standard formatting methods in C#

standard formatting methods

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 standard 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 Use ListView data to generate HTML TABLE code in C# to convert the results into the following HTML table.

Method Equivalent Result
ToLongDateString D Monday, August 18, 2014
ToLongTimeString T 5:03:16 PM
ToShortDateString d 8/18/2014
ToShortTimeString t 5:03 PM
ToString G 8/18/2014 5:03:16 PM


Download Example   Follow me on Twitter   RSS feed


This entry was posted in formatting, strings and tagged , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *