Use standard date and time formats in C#

standard date and time formats

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

Date Formats
Name Format Result
Short date d 8/18/2014
Long date D Monday, August 18, 2014
Month/day M or m August 18
Year/month Y or y August 2014
Time Formats
Name Format Result
Short time t 4:59 PM
Long time T 4:59:39 PM
Date/Time Formats
Name Format Result
Full date/time (short time) f Monday, August 18, 2014 4:59 PM
Full date/time (long time) F Monday, August 18, 2014 4:59:39 PM
General date/time (short time) g 8/18/2014 4:59 PM
General date/time (long time) G 8/18/2014 4:59:39 PM
Round-trip date/time O or o 2014-08-18T16:59:39.4458600-06:00
RFC1123 R or r Mon, 18 Aug 2014 16:59:39 GMT
Sortable date/time s 2014-08-18T16:59:39
Universal sortable date/time u 2014-08-18T16:59:39
Universal full date/time U 2014-08-18 16:59:39Z

Download Example   Follow me on Twitter   RSS feed

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

1 Response to Use standard date and time formats in C#

  1. Pingback: Use custom date and time formats in C# -

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.