DateTime.ToString() Method in C#

The DateTime.ToString() method in C# converts a DateTime object to its string representation. This method provides multiple overloads to format the date and time according to different requirements using format strings and culture-specific formatting.

Syntax

The DateTime.ToString() method has four overloads −

ToString()
ToString(String)
ToString(IFormatProvider)
ToString(String, IFormatProvider)

Parameters

  • format − A standard or custom date and time format string.

  • provider − An object that supplies culture-specific formatting information.

Return Value

Returns a string representation of the current DateTime object formatted according to the specified format string and culture information.

Using ToString() with Default Format

The parameterless ToString() method uses the default format for the current culture −

using System;

public class Demo {
   public static void Main() {
      DateTime d = new DateTime(2019, 11, 11, 7, 11, 25);
      Console.WriteLine("Date = {0}", d);
      string str = d.ToString();
      Console.WriteLine("String representation = {0}", str);
   }
}

The output of the above code is −

Date = 11/11/2019 7:11:25 AM
String representation = 11/11/2019 7:11:25 AM

Using ToString() with Standard Format Strings

Standard format strings provide predefined formatting patterns −

using System;

public class Demo {
   public static void Main() {
      DateTime d = new DateTime(2019, 11, 11, 7, 11, 25);
      Console.WriteLine("Date = {0}", d);
      string str = d.ToString("f");
      Console.WriteLine("Full date/time pattern (short time) = {0}", str);
      Console.WriteLine("Long date pattern = {0}", d.ToString("D"));
      Console.WriteLine("Short date pattern = {0}", d.ToString("d"));
      Console.WriteLine("Long time pattern = {0}", d.ToString("T"));
   }
}

The output of the above code is −

Date = 11/11/2019 7:11:25 AM
Full date/time pattern (short time) = Monday, November 11, 2019 7:11 AM
Long date pattern = Monday, November 11, 2019
Short date pattern = 11/11/2019
Long time pattern = 7:11:25 AM

Using ToString() with Custom Format Strings

Custom format strings allow precise control over the output format −

using System;

public class Demo {
   public static void Main() {
      DateTime d = new DateTime(2019, 11, 11, 7, 11, 25);
      Console.WriteLine("Custom formats:");
      Console.WriteLine("yyyy-MM-dd: {0}", d.ToString("yyyy-MM-dd"));
      Console.WriteLine("dd/MM/yyyy HH:mm:ss: {0}", d.ToString("dd/MM/yyyy HH:mm:ss"));
      Console.WriteLine("MMMM dd, yyyy: {0}", d.ToString("MMMM dd, yyyy"));
      Console.WriteLine("dddd, hh:mm tt: {0}", d.ToString("dddd, hh:mm tt"));
   }
}

The output of the above code is −

Custom formats:
yyyy-MM-dd: 2019-11-11
dd/MM/yyyy HH:mm:ss: 11/11/2019 07:11:25
MMMM dd, yyyy: November 11, 2019
dddd, hh:mm tt: Monday, 07:11 AM

Common Format Strings

Format String Description Example Output
d Short date pattern 11/11/2019
D Long date pattern Monday, November 11, 2019
f Full date/time pattern (short time) Monday, November 11, 2019 7:11 AM
F Full date/time pattern (long time) Monday, November 11, 2019 7:11:25 AM
yyyy-MM-dd Custom ISO date format 2019-11-11

Conclusion

The DateTime.ToString() method in C# provides flexible formatting options for converting date and time values to strings. Use standard format strings for common patterns or custom format strings for specific requirements, with optional culture-specific formatting through IFormatProvider.

Updated on: 2026-03-17T07:04:35+05:30

563 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements