Stats

Contributors: 9
2016-09-29
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook

Format dates in strings

Download c# eBook

Example

var date = new DateTime(2015, 11, 11);
var str = $"It's {date:MMMM d, yyyy}, make a wish!";
System.Console.WriteLine(str);

You can also use the DateTime.ToString method to format the DateTime object. This will produce the same output as the code above.

var date = new DateTime(2015, 11, 11);
var str = date.ToString("MMMM d, yyyy");
str = "It's " + str + ", make a wish!";
Console.WriteLine(str);

Output:

It's November 11, 2015, make a wish!

Live Demo on .NET Fiddle

Live Demo using DateTime.ToString

Note: MM stands for months and mm for minutes. Be very careful when using these as mistakes can introduce bugs that may be difficult to discover.