DateTime.ToString Method (String)

Converts the value of the current DateTime object to its equivalent string representation using the specified format and the formatting conventions of the current culture.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public string ToString(
	string format
)

Parameters

format
Type: System.String

A standard or custom date and time format string (see Remarks).

Return Value

Type: System.String
A string representation of value of the current DateTime object as specified by format.

ExceptionCondition
FormatException

The length of format is 1, and it is not one of the format specifier characters defined for DateTimeFormatInfo.

-or-

format does not contain a valid custom format pattern.

ArgumentOutOfRangeException

The date and time is outside the range of dates supported by the calendar used by the current culture.

The ToString(String) method returns the string representation of a date and time value in a specific format that uses the formatting conventions of the current culture; for more information, see CultureInfo.CurrentCulture.

The format parameter should contain either a single format specifier character (see Standard Date and Time Format Strings) or a custom format pattern (see Custom Date and Time Format Strings) that defines the format of the returned string. If format is null or an empty string, the general format specifier, 'G', is used.

Some uses of this method include:

  • Getting a string that displays the date and time in the current culture’s short date and time format. To do this, you use the “G” format specifier.

  • Getting a string that contains only the month and year. To do this, you use the “MM/yyyy” format string. The format string uses the current culture’s date separator.

  • Getting a string that contains the date and time in a specific format. For example, the “MM/dd/yyyy HH:mm” format string displays the date and time string in a fixed format such as “19//03//2013 18:06". The format string uses “/” as a fixed date separator regardless of culture-specific settings.

  • Getting a date in a condensed format that could be used for serializing a date string. For example, the "yyyyMMdd" format string displays a four-digit year followed by a two-digit month and a two-digit day with no date separator.

The following example uses these three format strings to display a date and time value by using the conventions of the en-US and fr-FR cultures.

using System;
using System.Globalization;
using System.Threading;

public class Example
{
   public static void Main()
   {
      String[] formats = { "G", "MM/yyyy", @"MM\/dd\/yyyy HH:mm",
                           "yyyyMMdd" };
      String[] cultureNames = { "en-US", "fr-FR" };
      DateTime date = new DateTime(2015, 8, 18, 13, 31, 17);
      foreach (var cultureName in cultureNames) {
         var culture = new CultureInfo(cultureName);
         CultureInfo.CurrentCulture = culture;
         Console.WriteLine(culture.NativeName);
         foreach (var format in formats)
            Console.WriteLine("   {0}: {1}", format,
                              date.ToString(format));
         Console.WriteLine();
      }
   }
}
// The example displays the following output: 
//       English (United States) 
//          G: 8/18/2015 1:31:17 PM 
//          MM/yyyy: 08/2015 
//          MM\/dd\/yyyy HH:mm: 08/18/2015 13:31 
//          yyyyMMdd: 20150818 
// 
//       français (France) 
//          G: 18/08/2015 13:31:17 
//          MM/yyyy: 08/2015 
//          MM\/dd\/yyyy HH:mm: 08/18/2015 13:31 
//          yyyyMMdd: 20150818

Notes to Callers

The ToString(String) method returns the string representation of the date and time in the calendar used by the current culture. If the value of the current DateTime instance is earlier than Calendar.MinSupportedDateTime or later than Calendar.MaxSupportedDateTime, the method throws an ArgumentOutOfRangeException. The following example provides an illustration. It attempts to format a date that is outside the range of the HebrewCalendar class when the current culture is Hebrew (Israel).

using System;
using System.Globalization;
using System.Threading;

public class Example
{
   public static void Main()
   {
      DateTime date1 = new DateTime(1550, 7, 21);
      CultureInfo dft;
      CultureInfo heIL = new CultureInfo("he-IL");
      heIL.DateTimeFormat.Calendar = new HebrewCalendar();

      // Change current culture to he-IL.
      dft = Thread.CurrentThread.CurrentCulture;
      Thread.CurrentThread.CurrentCulture = heIL;

      // Display the date using the current culture's calendar.             
      try {
         Console.WriteLine(date1.ToString("G"));
      }   
      catch (ArgumentOutOfRangeException) {
         Console.WriteLine("{0} is earlier than {1} or later than {2}", 
                           date1.ToString("d", CultureInfo.InvariantCulture), 
                           heIL.DateTimeFormat.Calendar.MinSupportedDateTime.ToString("d", CultureInfo.InvariantCulture),  
                           heIL.DateTimeFormat.Calendar.MaxSupportedDateTime.ToString("d", CultureInfo.InvariantCulture)); 
      }

      // Restore the default culture.
      Thread.CurrentThread.CurrentCulture = dft;
   }
}
// The example displays the following output: 
//    07/21/1550 is earlier than 01/01/1583 or later than 09/29/2239

The following example uses each of the standard date and time format strings and a selection of custom date and time format strings to display the string representation of a DateTime value. The thread current culture for the example is en-US.

using System;

public class DateToStringExample
{
   public static void Main()
   {
      DateTime dateValue = new DateTime(2008, 6, 15, 21, 15, 07);
      // Create an array of standard format strings. 
      string[] standardFmts = {"d", "D", "f", "F", "g", "G", "m", "o", 
                               "R", "s", "t", "T", "u", "U", "y"};
      // Output date and time using each standard format string. 
      foreach (string standardFmt in standardFmts)
         Console.WriteLine("{0}: {1}", standardFmt, 
                           dateValue.ToString(standardFmt));
      Console.WriteLine();

      // Create an array of some custom format strings. 
      string[] customFmts = {"h:mm:ss.ff t", "d MMM yyyy", "HH:mm:ss.f", 
                             "dd MMM HH:mm:ss", @"\Mon\t\h\: M", "HH:mm:ss.ffffzzz" };
      // Output date and time using each custom format string. 
      foreach (string customFmt in customFmts)
         Console.WriteLine("'{0}': {1}", customFmt,
                           dateValue.ToString(customFmt));
   }
}
// This example displays the following output to the console: 
//       d: 6/15/2008 
//       D: Sunday, June 15, 2008 
//       f: Sunday, June 15, 2008 9:15 PM 
//       F: Sunday, June 15, 2008 9:15:07 PM 
//       g: 6/15/2008 9:15 PM 
//       G: 6/15/2008 9:15:07 PM 
//       m: June 15 
//       o: 2008-06-15T21:15:07.0000000 
//       R: Sun, 15 Jun 2008 21:15:07 GMT 
//       s: 2008-06-15T21:15:07 
//       t: 9:15 PM 
//       T: 9:15:07 PM 
//       u: 2008-06-15 21:15:07Z 
//       U: Monday, June 16, 2008 4:15:07 AM 
//       y: June, 2008 
//        
//       'h:mm:ss.ff t': 9:15:07.00 P 
//       'd MMM yyyy': 15 Jun 2008 
//       'HH:mm:ss.f': 21:15:07.0 
//       'dd MMM HH:mm:ss': 15 Jun 21:15:07 
//       '\Mon\t\h\: M': Month: 6 
//       'HH:mm:ss.ffffzzz': 21:15:07.0000-07:00

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft