Use standard date and time formats in C#

You can use numeric formatting characters to display date and time values in particular formats. These formats work with a variable's ToString method as well as with String.Format. For example, the code DateTime.Now.ToString("f") returns the current date and time in full date/time format with a short time.

This example builds a ListView that demonstrates the various standard date and time formats. 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.

Date Formats
Name Format Result
Short date d 3/5/2010
Long date D Friday, March 05, 2010
Month/day M or m March 05
Year/month Y or y March, 2010
Time Formats
Name Format Result
Short time t 7:08 AM
Long time T 7:08:34 AM
Date/Time Formats
Name Format Result
Full date/time (short time) f Friday, March 05, 2010 7:08 AM
Full date/time (long time) F Friday, March 05, 2010 7:08:34 AM
General date/time (short time) g 3/5/2010 7:08 AM
General date/time (long time) G 3/5/2010 7:08:34 AM
Round-trip date/time O or o 2010-03-05T07:08:34.4406435-07:00
RFC1123 R or r Fri, 05 Mar 2010 07:08:34 GMT
Sortable date/time s 2010-03-05T07:08:34
Universal sortable date/time u 2010-03-05T07:08:34
Universal full date/time U 2010-03-05 07:08:34Z

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.