Languages
[Edit]
EN

C# / .NET - convert DateTime to string fromat

2 points
Created by:
Marcin
15680

In C# / .NET it is possible to convert DateTime object to string with formatting in few ways.

1. DateTime.ToString method example

using System;

public static class Program
{
	public static void Main(string[] args)
	{
		DateTime now = DateTime.Now;

		Console.WriteLine(now.ToString("dddd, dd MMMM yyyy HH:mm"));
		Console.WriteLine(now.ToString("dddd, dd MMMM yyyy HH:mm tt"));
		Console.WriteLine(now.ToString("dddd, dd MMMM yyyy HH:mm:ss"));
		Console.WriteLine(now.ToString("MM/dd/yyyy HH:mm"));
		Console.WriteLine(now.ToString("MM/dd/yyyy HH:mm tt"));
		Console.WriteLine(now.ToString("MM/dd/yyyy HH:mm:ss"));
	}
}

Output:

Wednesday, 14 August 2019 22:28
Wednesday, 14 August 2019 22:28 PM
Wednesday, 14 August 2019 22:28:02
08/14/2019 22:28
08/14/2019 22:28 PM
08/14/2019 22:28:02
Notes: 
  • this example has been run with independent culture.
  • DateTime.ToString method uses format description - see for examples bellow

2. String.Format Method examples

2.1. Usage example

using System;

public static class Program
{
	public static void Main(string[] args)
	{
		DateTime now = DateTime.Now;

		Console.WriteLine(string.Format("{0:dddd, dd MMMM yyyy HH:mm}", now));
		Console.WriteLine(string.Format("{0:dddd, dd MMMM yyyy HH:mm tt}", now));
		Console.WriteLine(string.Format("{0:dddd, dd MMMM yyyy HH:mm:ss}", now));
		Console.WriteLine(string.Format("{0:MM/dd/yyyy HH:mm}", now));
		Console.WriteLine(string.Format("{0:MM/dd/yyyy HH:mm tt}", now));
		Console.WriteLine(string.Format("{0:MM/dd/yyyy HH:mm:ss}", now));
	}
}

Output:

Wednesday, 14 August 2019 22:30
Wednesday, 14 August 2019 22:30 PM
Wednesday, 14 August 2019 22:30:17
08/14/2019 22:30
08/14/2019 22:30 PM
08/14/2019 22:30:17
Notes: 
  • this example has been run with independent culture.
  • String.Format method uses argument index and format description delimited with ":" character - see for examples bellow

2.2. Own custom formats

FORMAT                            TEXT
------------------------------------------------------------------------
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-dd'T'HH:mm:ss             2018-02-15T08:21:34
yyyy-MM-dd'T'HH: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
Notes:
  • To escape some phrases, apostrophes should be used like in this example:
    ddd, dd MMM yyy HH:mm:ss 'GMT'      Thu, 15 Feb 2018 08:21:34 GMT
    Where M by default represents number of month in DateTime.ToString format.
  • For safe, each phrases that should be not interpreted should be placed between apostrophes.

References

  1. DateTime.ToString Method - Microsoft Docs
  2. String.Format Method - Microsoft Docs

C# - DateTime conversion

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