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.

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

See also:

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.