c-sharp-datetime-formatC# makes working with dates and times easy with the DateTime class.  There are structures, methods and properties that let you perform date-time operations in your program. Among many operations, date-time formatting is used to present a date to the user without manually formatting your dates.

Want to learn the C# fundamentals? Learn at Udemy.

Before going through the formatting techniques, you should have a basic knowledge of C#. Here are some basic points to know about DateTime:

Example:

DateTime time = DateTime.Now;
DateTime date2 = DateTime.Today;
DateTime date3 = new DateTime(2014, 4, 21);
DateTime date4 = new DateTime(2014, 3, 15, 5, 4, 9);
Console.WriteLine(time);
Console.WriteLine(date2);
Console.WriteLine(date3);
Console.WriteLine(date4);

Output:

4/21/2014 2:09:43 PM

4/21/2014 12:00:00 AM

4/21/2014 12:00:00 AM

3/15/2014 5:04:09 AM

 

Example:

DateTime time = DateTime.Now;
String s;
s = time.ToString("yyyy-MM-dd HH:mm tt");

 

Example:

String s;
s = "2014-08-04 12:00 PM";
DateTime dt = new DateTime();
dt = DateTime.ParseExact(s, "yyyy-MM-dd HH:mm tt", null);

 

Formatting Your Dates

You can format your dates and times in several ways.

  1. You can format them using DateTime methods
  2. You can use the “string” formatting method.
  3. You can also create your own, custom date and time format functions.

Formats Using DateTime Methods

The most popular way to format DateTime variables is using the internal class methods.

The following are some examples for formatting using DateTime methods:

Example:

DateTime dt = new DateTime(2014, 05, 01);
Console.WriteLine(dt.ToLongDateString());

         Output:  Thursday, May 01, 2014

Example:

DateTime dt = new DateTime(2014, 05, 01);
Console.WriteLine(dt.ToShortDateString());

Output:

5/1/2014

 

Example:

DateTime dt = new DateTime(2014, 05, 01, 12, 05, 54);
Console.WriteLine(dt.ToLongTimeString());

Output:  12:05:54 PM

 

Example:

DateTime dt = new DateTime(2014, 05, 01, 12, 05, 54);
Console.WriteLine(dt.ToShortTimeString());

Output: 15:05 PM

 

Example:

DateTime dt = new DateTime(2014, 05, 01, 12, 05, 54);
Console.WriteLine(dt.ToString());

Output: 5/1/2014 12:05:54 PM

 

Standard Date and Time Formats

When you want to format a specific date and time, you use a single-character to indicate the type of output you want to display. The following table shows you these single-characters and their corresponding output:

Table: Single-character format specifiers

Format Specifier     Description                                    Example

d                                Short date                                      21/4/2014

D                                Long date                                       05 March 2014

t                                 Short Time                                     05:10

T                                Long Time                                       05:10:10

f                                 Full date-time (short)                      09 June 2014 11:10

F                                 Full date-time (long)                      09 June 2014 11:10:10

g                                 Default date and time (short)           09/11/2014 11:10

G                                 Default date and time (long)           09/11/2014 11:10:10

M                                 Day and Month                                    07 July

r                                  RFC1123 date                              Mon, 08 Apr 2010 19:07:21 GMT

s                                  Sortable date-time                       2008-09-14T15:08:08

u                               Universal time, local time zone          2014-09-07 10:10:10Z

U                                  Universal full date/time            Tuesday, July 13, 2014 8:40:21 PM

Y, y                               Month and Year                                   June 2014

You use the single-character format specifier with the ToString() method. The format specifier character is sent as an argument to the ToString() method.

Example:

DateTime dt = new DateTime(2014, 05, 01, 12, 05, 54);
Console.WriteLine(dt.ToString("d"));
Console.WriteLine(dt.ToString("D"));
Console.WriteLine(dt.ToString("t"));
Console.WriteLine(dt.ToString("T"));
Console.WriteLine(dt.ToString("f"));
Console.WriteLine(dt.ToString("F"));
Console.WriteLine(dt.ToString("g"));
Console.WriteLine(dt.ToString("G"));

Output:

5/1/2014

Thursday, May 01, 2014

12:05 PM

12:05:54 PM

Thursday, May 01, 2014 12:05 PM

Thursday, May 01, 2014 12:05:54 PM

5/1/2014 12:05 PM

5/1/2014 12:05:54 PM

 

Custom Date and Time Formats

You can represent date and time in various formats. There are many format specifiers that work with the ToString() method to format date and time values. Custom date and time format strings are used to achieve much more flexibility. The use of each custom format string is explained one by one with appropriate code examples. “d”, “dd”, “ddd”, “dddd”, “M”, “y” are some of the custom format strings.

Example:

DateTime dt = new DateTime(2014, 05, 01, 12, 05, 54);
Console.WriteLine(dt.ToString("dddd"));

Output:

Thursday

 

Using Separators

Colon “:” is used as the time separator while “/” is used as the date separator.

Example:

DateTime dt = new DateTime(2014, 05, 01, 12, 05, 54);
Console.WriteLine(dt.ToString("dd/MM/yyy hh:mm:ss"));

Output: 01/05/2014 12:05:54

 

Day Format

A day is represented in several forms using the format specifiers or format strings “d”, “dd”, “ddd”, “dddd”.

Format specifiers d, dd, ddd, dddd:

DateTime dt = new DateTime(2013, 03, 06, 11, 06, 14);
Console.WriteLine(dt.ToString("d ")); //Give a space after d
Console.WriteLine(dt.ToString("dd"));
Console.WriteLine(dt.ToString("ddd"));
Console.WriteLine(dt.ToString("dddd"));

Output:

6

06

Wed

Wednesday

 

Month Format

Month format strings are used to represent a month in several forms.

Format specifiers M, MM, MMM, MMMM:

DateTime dt = new DateTime(2011, 08, 03, 10, 09, 10);
Console.WriteLine(dt.ToString("M"));
Console.WriteLine(dt.ToString("MM"));
Console.WriteLine(dt.ToString("MMM"));
Console.WriteLine(dt.ToString("MMMM"));

Output:

8

08

Aug

August

 

Year Format

Year format strings are used to represent a year in several forms.

Format specifiers y, yy, yyy, yyyy:

DateTime dt = new DateTime(2005, 01, 01, 01, 01, 01);
Console.WriteLine(dt.ToString("y "));
Console.WriteLine(dt.ToString("yy"));
Console.WriteLine(dt.ToString("yyy"));
Console.WriteLine(dt.ToString("yyyy"));

Output:

5

05

2005

2005

 

Hour, Minute and Second Formats

Format specifiers h, hh, H, HH:

DateTime time = DateTime.Now;
Console.WriteLine(time.ToString("hh:mm:ss"));
Console.WriteLine(time.ToString("h:mm:ss"));
Console.WriteLine(time.ToString("mm"));
Console.WriteLine(time.ToString("hh"));

 

AM/PM

If you want to display AM or PM with the date, use the “tt” string.

Example:

DateTime time = DateTime.Now;
Console.WriteLine(time.ToString("hh:mm:ss tt"));

Output:

01:37:07 PM

 

AD/BC

Display AD or BC with the date using the “gg” string in the following way.

Example:

DateTime time = DateTime.Now;
Console.WriteLine(time.ToString("hh:mm:ss tt gg"));

Output:

01:37:51 PM A.D.

 

Do you want to learn more about C#? Learn the language at Udemy.

Top courses in C#

How to Program Voxel Worlds Like Minecraft with C# in Unity
Penny de Byl, Penny @Holistic3D.com
4.6 (1,239)
Learn C# for Beginners Crash Course
Tim Buchalka, Jean-Paul Roberts, Tim Buchalka's Learn Programming Academy
4.7 (539)
Design Patterns in C# Made Simple
Zoran Horvat
4.7 (49)
Highest Rated
Complete C# Unity Game Developer 2D
Ben Tristem, GameDev.tv Team, Rick Davidson
4.6 (77,201)
Bestseller
Complete C# Unity Game Developer 3D
Ben Tristem, Rick Davidson, GameDev.tv Team
4.6 (21,265)
Bestseller

More C# Courses

C# students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.

Request a demo