Languages
[Edit]
EN

C# / .NET - toString DateTime format

8 points
Created by:
Marcin
15740

In this article, we're going to have a look at how to format date-time string from DateTime.ToString() method in C# / .NET.

Note: check below table of formats and results for them to see how it works.

Quick solution:

using System;
using System.Globalization;

public class Program
{
	public static void Main()
	{
		DateTime dt = new DateTime(2018, 2, 15, 8, 21, 34, 547);

        //                            |<--------- FORMAT -------->| 
		Console.WriteLine(dt.ToString("dddd, dd MMMM yyyy HH:mm"   ));
		Console.WriteLine(dt.ToString("dddd, dd MMMM yyyy HH:mm tt"));
	}
}

Output:

Thursday, 15 February 2018 08:21
Thursday, 15 February 2018 08:21 AM

Different ToString formats gives following results: 

/*

FORMAT                            RESULT

dddd, dd MMMM yyyy HH:mm          Thursday, 15 February 2018 08:21
dddd, dd MMMM yyyy HH:mm tt       Thursday, 15 February 2018 08:21 AM
dddd, dd MMMM yyyy HH:mm:ss       Thursday, 15 February 2018 08:21:34
dddd, dd MMMM yyyy HH:mm:ss tt    Thursday, 15 February 2018 08:21:34 AM
dddd, dd MMMM yyyy H:mm           Thursday, 15 February 2018 8:21
dddd, dd MMMM yyyy H:mm tt        Thursday, 15 February 2018 8:21 AM
dddd, dd MMMM yyyy H:mm:ss        Thursday, 15 February 2018 8:21:34
dddd, dd MMMM yyyy H:mm:ss tt     Thursday, 15 February 2018 8:21:34 AM
dddd, dd MMMM yyyy hh:mm          Thursday, 15 February 2018 08:21
dddd, dd MMMM yyyy hh:mm tt       Thursday, 15 February 2018 08:21 AM
dddd, dd MMMM yyyy hh:mm:ss       Thursday, 15 February 2018 08:21:34
dddd, dd MMMM yyyy hh:mm:ss tt    Thursday, 15 February 2018 08:21:34 AM
dddd, dd MMMM yyyy h:mm           Thursday, 15 February 2018 8:21
dddd, dd MMMM yyyy h:mm tt        Thursday, 15 February 2018 8:21 AM
dddd, dd MMMM yyyy h:mm:ss        Thursday, 15 February 2018 8:21:34
dddd, dd MMMM yyyy h:mm:ss tt     Thursday, 15 February 2018 8:21:34 AM
MM/dd/yyyy HH:mm                  02/15/2018 08:21
MM/dd/yyyy HH:mm tt               02/15/2018 08:21 AM
MM/dd/yyyy HH:mm:ss               02/15/2018 08:21:34
MM/dd/yyyy HH:mm:ss tt            02/15/2018 08:21:34 AM
MM/dd/yyyy H:mm                   02/15/2018 8:21
MM/dd/yyyy H:mm tt                02/15/2018 8:21 AM
MM/dd/yyyy H:mm:ss                02/15/2018 8:21:34
MM/dd/yyyy H:mm:ss tt             02/15/2018 8:21:34 AM
MM/dd/yyyy hh:mm                  02/15/2018 08:21
MM/dd/yyyy hh:mm tt               02/15/2018 08:21 AM
MM/dd/yyyy hh:mm:ss               02/15/2018 08:21:34
MM/dd/yyyy hh:mm:ss tt            02/15/2018 08:21:34 AM
MM/dd/yyyy h:mm                   02/15/2018 8:21
MM/dd/yyyy h:mm tt                02/15/2018 8:21 AM
MM/dd/yyyy h:mm:ss                02/15/2018 8:21:34
dddd, dd MMMM yy                  Thursday, 15 February 18
dddd, dd MMMM yyyy                Thursday, 15 February 2018
MM/dd/yy                          02/15/18
MM/dd/yyyy                        02/15/2018
MMMM dd                           February 15
yy-MM-dd                          18-02-15
yyyy-MM-dd                        2018-02-15
yy.MM.dd                          18.02.15
yyyy.MM.dd                        2018.02.15
dd.MM.yy                          15.02.18
dd.MM.yyyy                        15.02.2018
yyyy MMMM                         2018 February
ddd, MMM d, yyyy                  Thu, Feb 15, 2018
dddd, MMMM d, yyyy                Thursday, February 15, 2018
yyyy-MM-ddTHH:mm:ss               2018-02-15T08:21:34
yyyy-MM-ddTHH:mm:ss.fffffffK      2018-02-15T08:21:34.5470000
yyyyMMddHHmmss                    20180215082134
yyyyMMddHHmmssfffffff             201802150821345470000
ddd, dd MMM yyy HH:mm:ss 'GMT'    Thu, 15 Feb 2018 08:21:34 GMT
HH:mm                             08:21
HH:mm tt                          08:21 AM
HH:mm:ss                          08:21:34
HH:mm:ss tt                       08:21:34 AM
H:mm                               8:21
H:mm tt                            8:21 AM
H:mm:ss                            8:21:34
H:mm:ss tt                         8:21:34 AM
hh:mm                             08:21
hh:mm tt                          08:21 AM
hh:mm:ss                          08:21:34
hh:mm:ss tt                       08:21:34 AM
h:mm                               8:21
h:mm tt                            8:21 AM
h:mm:ss                            8:21:34


Where:
       H -  0-23 hours
      HH - 00-23 hours         (zeros are printed to keep two places)
       h -  0-12 hours
      hh - 00-12 hours         (zeros are printed to keep two places)
      mm - 00-59 minutes       (zeros are printed to keep two places)
      ss - 00-59 seconds       (zeros are printed to keep two places)
      tt - AM or PM
 fffffff - second fraction
fffffffK - second fraction

       d - day of month number
      dd - day of month number (zeros are printed to keep two places)
     ddd - short day name
    dddd - long day name
      MM - month number        (zeros are printed to keep two places)
     MMM - short month name
    MMMM - long month name
      yy - 00-99 year          (zeros are printed to keep two places)
     yyy - 00-999 year         (zeros are printed to keep two places)
    yyyy - 00-9999 year        (zeros are printed to keep two places)

  Charactes that are not interpreted: T - : . ,

  'text' - single apostrophe does not interpret text inside e.g. 'GMT' as GMT

*/
Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join