C#

DateTime format. It is the year 3057. We have hover cars and space elevators. We live in the future. But then a problem appears. We must format a date in a C# program.
M, uppercase. An uppercase "M" is a special one-char date code. When we pass an "M" as the format of ToString, we get the Month and the day of the month.

ToString:A DateTime is converted to a string with a format. We use the ToString method. This receives many formats.

Tip:This tutorial is long and repetitive. You may want to scroll extra fast. We cover common format codes individually at first.

Based on: .NET 4.6

C# program that uses M format

using System;
using static System.Console;

class Program
{
    static void Main()
    {
	// Create DateTime for March 14,3057.
	DateTime time = new DateTime(3057, 3, 14);
	// One M.
	WriteLine(time.ToString("M"));
    }
}

Output

March 14
MM, uppercase. With two uppercase M chars we have a component of a date. This prints the number of the month. So March is numbered 3 (it is the third month). It is my favorite month.
C# program that uses MM format

using System;
using static System.Console;

class Program
{
    static void Main()
    {
	// Use MM code.
	WriteLine(new DateTime(3057, 3, 14).ToString("MM"));
    }
}

Output

03
MMM, uppercase. With three M chars we have a 3-char date string. So if you want Mar instead of March this code is ideal. This is really getting interesting now.
C# program that uses MMM format

using System;
using static System.Console;

class Program
{
    static void Main()
    {
	// Use MMM code.
	WriteLine(new DateTime(3057, 3, 14).ToString("MMM"));
    }
}

Output

Mar
MMMM, uppercase. Now we add another "M." You probably thought three M chars was enough. But you were wrong. The .NET Framework is so powerful it can handle 4 M chars at once.

Tip:The MMMM format prints the entire month string like March. This is the verbose form.

C# program that uses MMMM code

using System;
using static System.Console;

class Program
{
    static void Main()
    {
	// Use MMMM code.
	WriteLine(new DateTime(3057, 3, 14).ToString("MMMM"));
    }
}

Output

March
D, lowercase. This is a single-char date code. A lowercase "d" char means the entire date in short form. So it lists month, day and year with slashes in between.
C# program that uses d format

using System;
using static System.Console;

class Program
{
    static void Main()
    {
	// Use d code.
	WriteLine(new DateTime(3057, 3, 14).ToString("d"));
    }
}

Output

3/14/3057
Dd, lowercase. This is a component of a date—the day of the month. Soon March 14, this will be the string representation of "14." A "dd" will be part of a larger format string.
C# program that uses dd format

using System;
using static System.Console;

class Program
{
    static void Main()
    {
	// Use dd code.
	WriteLine(new DateTime(3057, 3, 14).ToString("dd"));
    }
}

Output

14
Ddd, lowercase. This is the three-letter weekday string code. So we have Mon, Tue, Wed, Thu, Fri, Sat, Sun. This is an abbreviated day.

And:This is almost always part of a larger DateTime format string. It can be used for easy-to-read but still short representations.

C# that uses ddd format

using System;
using static System.Console;

class Program
{
    static void Main()
    {
	// Use ddd code.
	WriteLine(new DateTime(3057, 3, 14).ToString("ddd"));
    }
}

Output

Sat
Dddd, lowercase. With four lowercase "d" chars we have the full day name. This is most readable: we have Saturday and Sunday and all those other days too.
C# that uses dddd format

using System;
using static System.Console;

class Program
{
    static void Main()
    {
	// Use dddd code.
	WriteLine(new DateTime(3057, 3, 14).ToString("dddd"));
    }
}

Output

Saturday
Yy, lowercase. This is the short year format. So for 1996 this will read 96. For year 3057 this is 57. Only the last two digits of the year are kept.
C# that uses yy format

using System;
using static System.Console;

class Program
{
    static void Main()
    {
	// Use yy code.
	WriteLine(new DateTime(3057, 3, 14).ToString("yy"));
    }
}

Output

57
Yyy, lowercase. This is the full year code. So for the year 3057 we get the entire number 3057. Generally with format codes, the more chars means more detail and more readability.

And:The yyy year code is usually the one we want. With fewer digits, it is hard to tell some dates apart.

C# that uses yyy format

using System;
using static System.Console;

class Program
{
    static void Main()
    {
	// Use yyy code.
	WriteLine(new DateTime(3057, 3, 14).ToString("yyy"));
    }
}

Output

3057
M, lowercase. This has the same output as an uppercase M. It is a one-char date format string that gives us the month and the day.
C# that uses m format

using System;
using static System.Console;

class Program
{
    static void Main()
    {
	// Use m code.
	WriteLine(new DateTime(3057, 3, 14).ToString("m"));
    }
}

Output

March 14
Mm, lowercase. This is minutes. If the time is 12:30 (around lunch time) it will give us 30. It returns the number of minutes that are present in the time.

Note:With "mm" we do not count the entire time as minutes. Instead we just access the minutes component, separate of other time units.

Note 2:We print the results of this code with string interpolation syntax. We see the mm part is 19 for the given time.

String Interpolation
C# that uses mm format

using System;
using static System.Console;

class Program
{
    static void Main()
    {
	DateTime now = DateTime.Now;
	// Use mm code.
	string result = now.ToString("mm");
	WriteLine($"{now} [mm] = {result}");
    }
}

Output

12/5/2015 12:19:14 PM [mm] = 19
HH, uppercase. This is the hours component of the time. It is on a 24-hour scale, so it does not stop at 12. With HH, 1 PM is the hour 13.
C# that uses HH format

using System;
using static System.Console;

class Program
{
    static void Main()
    {
	DateTime now = DateTime.Now;
	// Use HH code.
	string result = now.ToString("HH");
	WriteLine($"{now} [HH] = {result}");
    }
}

Output

12/5/2015 1:34:26 PM [HH] = 13
Hh, lowercase. This is the hours component of the time, but on a 12-hour scale. So here 1 PM is represented as 1, not 13. The best format for hours depends entirely on what you are doing.
C# that uses hh format

using System;
using static System.Console;

class Program
{
    static void Main()
    {
	DateTime now = DateTime.Now;
	// Use hh code.
	string result = now.ToString("hh");
	WriteLine($"{now} [hh] = {result}");
    }
}

Output

12/5/2015 1:33:59 PM [hh] = 01
Example, complex format. Now let us combine the above format codes into a more complex format. Here we use a specific formatting string with DateTime and ToString.

Note:The letters in the format string specify the output. And the final part describes the format patterns.

C# that uses DateTime format

using System;

class Program
{
    static void Main()
    {
	DateTime time = DateTime.Now;             // Use current time.
	string format = "MMM ddd d HH:mm yyyy";   // Use this format.
	Console.WriteLine(time.ToString(format)); // Write to console.
    }
}

Output

Feb Fri 27 11:41 2009

Format string pattern

MMM     display three-letter month
ddd     display three-letter day of the WEEK
d       display day of the MONTH
HH      display two-digit hours on 24-hour scale
mm      display two-digit minutes
yyyy    display four-digit year
Example, modified format. Here we modify the format string in the above example to get different output. We change some of the fields so the resulting string value is shorter.

Note:You will also need to specify a format string when using DateTime.ParseExact and DateTime.ParseExact.

C# that uses different format

using System;

class Program
{
    static void Main()
    {
	DateTime time = DateTime.Now;
	string format = "M d h:mm yy";
	Console.WriteLine(time.ToString(format));
    }
}

Output

2 27 11:48 09

Format string pattern

M       display one-digit month number
d       display one-digit day of the month
h       display one-digit hour on 12-hour scale
mm      display two-digit minutes
yy      display two-digit year
Single-char format. We use a char with ToString or DateTime.ParseExact to specify a preset format. These are standard formats. They are useful in many programs.DateTime.Parse
C# that tests formats

using System;

class Program
{
    static void Main()
    {
	DateTime now = DateTime.Now;
	Console.WriteLine(now.ToString("d"));
	Console.WriteLine(now.ToString("D"));
	Console.WriteLine(now.ToString("f"));
	Console.WriteLine(now.ToString("F"));
	Console.WriteLine(now.ToString("g"));
	Console.WriteLine(now.ToString("G"));
	Console.WriteLine(now.ToString("m"));
	Console.WriteLine(now.ToString("M"));
	Console.WriteLine(now.ToString("o"));
	Console.WriteLine(now.ToString("O"));
	Console.WriteLine(now.ToString("s"));
	Console.WriteLine(now.ToString("t"));
	Console.WriteLine(now.ToString("T"));
	Console.WriteLine(now.ToString("u"));
	Console.WriteLine(now.ToString("U"));
	Console.WriteLine(now.ToString("y"));
	Console.WriteLine(now.ToString("Y"));
    }
}

Output

d    2/27/2009
D    Friday, February 27, 2009
f    Friday, February 27, 2009 12:11 PM
F    Friday, February 27, 2009 12:12:22 PM
g    2/27/2009 12:12 PM
G    2/27/2009 12:12:22 PM
m    February 27
M    February 27
o    2009-02-27T12:12:22.1020000-08:00
O    2009-02-27T12:12:22.1020000-08:00
s    2009-02-27T12:12:22
t    12:12 PM
T    12:12:22 PM
u    2009-02-27 12:12:22Z
U    Friday, February 27, 2009 8:12:22 PM
y    February, 2009
Y    February, 2009
Date strings. Here we see the ToLongDateString, ToLongTimeString, ToShortDateString, and ToShortTimeString methods on DateTime. These methods use formats.

Note:These methods are equivalent to the lowercase and uppercase D and T methods shown in the example above.

Note 2:The default ToString on DateTime is equivalent to the "G" formatting string. ToString("G") and ToString() do the same thing.

string.Format
C# that uses ToString methods

using System;

class Program
{
    static void Main()
    {
	DateTime now = DateTime.Now;
	Console.WriteLine(now.ToLongDateString());  // Equivalent to D.
	Console.WriteLine(now.ToLongTimeString());  // Equivalent to T.
	Console.WriteLine(now.ToShortDateString()); // Equivalent to d.
	Console.WriteLine(now.ToShortTimeString()); // Equivalent to t.
	Console.WriteLine(now.ToString());
    }
}

Output

ToLongDateString     Friday, February 27, 2009
ToLongTimeString     12:16:59 PM
ToShortDateString    2/27/2009
ToShortTimeString    12:16 PM
ToString             2/27/2009 12:16:59 PM
Seconds format. Seconds are formatted with f, F and s. The uppercase F changes how trailing zeros are displayed. And the lowercase s changes how leading zeros are displayed.

Lowercase f:Use the lowercase f to indicate the seconds to one digit length. Use "ff" to indicate the seconds to two digits.

Note:The uppercase F patterns do the same but work differently on trailing zeros.

Lowercase s:The lowercase s displays seconds. With ss we always want two digits, such as 00-59.

C# that uses s format

using System;
using static System.Console;

class Program
{
    static void Main()
    {
	DateTime now = DateTime.Now;
	// Use space after s to avoid one-char date format.
	string result = now.ToString("s ");
	WriteLine($"{now} [s] = {result}");
    }
}

Output

12/5/2015 2:04:17 PM [s] = 17
Minutes format. We use the lowercase m or mm for minutes. Two lowercase ms has a leading zero if the number is only one digit long. This is the only difference.

Tip:The two ms means that there are always two digits displayed, with a leading zero if necessary.


Hours format. For hours we use "j" and "H." You can use one or two characters. The lowercase h is used for a 12-hour clock. The uppercase H is a 24-hour clock.

Value h:Display the hours in one digit if possible. If the hours is greater than 9, it will display two digits. Range is 1-12.

Value hh:Display the hours in two digits always, even if the hour is one digit. The range here will be 01-12.

Value H:This represents the hours in a range of 0-23, which is called military time in some parts of the world.

Value HH:This returns the hours in a range of 00-23. With a single H, there is always a leading zero if the number is one digit.

Statement that prints hours: C#

// Assume it is 1 PM.
Console.WriteLine(DateTime.Now.ToString("HH"));

Output

13
Day format. For days we use one to four d chars. One "d" and "dd" indicate the day of the month, while "ddd" and "dddd" indicate the day of the week, in a word.

Value d:Use this to specify the numeric value for the day of the month. It will be one or two digits long.

Value dd:This is the same as a single d, except there are always two digits, with a leading 0 prepended if necessary.

Value ddd:This displays a three-letter string that indicates the current day of the week.

Value dddd:This displays the full string for the day of the week. An example would be "Saturday."

C# that uses ddd and dddd

using System;

class Program
{
    static void Main()
    {
	DateTime time = new DateTime(2000, 1, 1);
	// Console.WriteLine and string.Format can handle dates.
	Console.WriteLine("Two letters: {0:ddd}", time);
	Console.WriteLine("Three letters: {0:dddd}", time);
    }
}

Output

Two letters: Sat
Three letters: Saturday
Month format. The month is formatted with an uppercase M. One and two Ms display numeric representations. Three and four Ms display string representations.

M, MM:These display the months in numeric form. One M does not have a leading zero on it. Two Ms have a leading zero.

MMM:This displays the three-letter form of the month represented in the DateTime (like "Jan").

MMMM:This displays the full month string, properly capitalized. An example is "January".


Year format. The year format uses the lowercase y letter. We can put one to five characters for the year. It is unlikely we will need five characters.

Tip:For year strings, we use the values "y" through "yyyyy". These display the year to different digits.

However:In your programs, you won't need three digits for the year (yyy), or five.


Misc. Even more DateTime formatting characters are available. You can change how AM and PM are displayed. You can display AD and BC. You can show the UTC offset and even display time zones.

Value t:Use the lowercase t to indicate A, when the time is in the AM, and P, for when the time is in PM.

Value tt:Use two lowercase tts to display the full AM or PM string. You will normally want this for displaying the string to a user.

Value gg:Use this to display AD on your date. It is unlikely that this will be BC in most programs.

Value k:Use this to display time zone information. This is often not needed in programs.

Values z, zz, zzz:These represent the offset from the UTC time on the local operating system.

Colon and slash:The colon is the time separator ":". The slash the date separator "/".


Complete day. Often we need to display the complete day of the week, and the four ds together will do this. With Console.WriteLine, we print all seven day strings we get from the dddd.Console.WriteLine
C# that shows day strings

using System;

class Program
{
    static void Main()
    {
	DateTime now = DateTime.Today;
	for (int i = 0; i < 7; i++)
	{
	    Console.WriteLine(now.ToString("dddd"));
	    now = now.AddDays(1);
	}
    }
}

Output

Thursday
Friday
Saturday
Sunday
Monday
Tuesday
Wednesday
Three-letter days. We can display the day of the week in a three-letter form. Here we see a simple program that does this. Results vary based on the current language.
C# that tests days

using System;

class Program
{
    static void Main()
    {
	DateTime now = DateTime.Today;
	for (int i = 0; i < 7; i++)
	{
	    Console.WriteLine(now.ToString("ddd"));
	    now = now.AddDays(1);
	}
    }
}

Output

Thu
Fri
Sat
Sun
Mon
Tue
Wed
Era. We can display the date with the era or period—usually AD or BC. It is unlikely that you will need to use BC, except in a rare theoretical application. Here is what two gs will print.
Statement that prints era: C#

Console.WriteLine(DateTime.Now.ToString("gg"));

Output

A.D.
Month. Programs have different formatting requirements for month names. Sometimes, the first three letters of the name are needed. This helps in tabular or spreadsheet displays.DateTime.Month
AM, PM. When you specify one t, you can get the first letter of the AM or PM string. This is equivalent to using Substring or getting the first char of the tt string.

Tip:There is a space at the end of the format string—the value "t" can mean something else in the format string.

Next:We show a program that gets the string AM or PM in DateTime ToString code.

Note:There are no periods in the output of tt. If you require periods in your AM or PM, you would have to manipulate the string.

C# that displays AM and PM

using System;

class Program
{
    static void Main()
    {
	DateTime now = DateTime.Now;
	for (int i = 0; i < 2; i++)
	{
	    Console.WriteLine(now.ToString("tt "));
	    now = now.AddHours(12);
	}
    }
}

Output

PM
AM
Year. We can vary the number of digits displayed in the year string. We will want to use y, yy, or yyyy for our programs. But five ys is also possible.

Sometimes:Two ys is useful for a user-oriented program, but for back-end code, we will want to use four ys.

C# that displays years

using System;

class Program
{
    static void Main()
    {
	DateTime now = DateTime.Now;
	Console.WriteLine(now.ToString("y "));
	Console.WriteLine(now.ToString("yy"));
	Console.WriteLine(now.ToString("yyy"));
	Console.WriteLine(now.ToString("yyyy"));
	Console.WriteLine(now.ToString("yyyyy"));
    }
}

Output

9
09
2009
2009
02009
Many DateTime formats can be used. We covered single-letter preset format strings. And we handled more complicated, custom strings with character codes.