DateTime.ToString Method (String, IFormatProvider)

Converts the value of the current DateTime object to its equivalent string representation using the specified format and culture-specific format information.

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

public string ToString(
	string format,
	IFormatProvider provider
)

Parameters

format
Type: System.String

A standard or custom date and time format string.

provider
Type: System.IFormatProvider

An object that supplies culture-specific formatting information.

Return Value

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

Implements

IFormattable.ToString(String, IFormatProvider)

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 provider.

The format parameter can 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). If format is null or an empty string (""), the standard format specifier, "G", is used.

The provider parameter defines the pattern that corresponds to the standard format specifiers, as well as the symbols and names of date and time components. The provider parameter can be any of the following:

If provider is null, the DateTimeFormatInfo associated with the current culture is used. For more information, see CultureInfo.CurrentCulture.

Notes to Callers

The ToString(String, IFormatProvider) method returns the string representation of the date and time in the calendar used by the provider parameter. Its calendar is defined by the Calendar property. 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 UmAlQuraCalendar class.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      CultureInfo arSA = new CultureInfo("ar-SA");
      arSA.DateTimeFormat.Calendar = new UmAlQuraCalendar(); 
      DateTime date1 = new DateTime(1890, 9, 10);

      try {
         Console.WriteLine(date1.ToString("d", arSA));
      }   
      catch (ArgumentOutOfRangeException) {
         Console.WriteLine("{0:d} is earlier than {1:d} or later than {2:d}", 
                           date1, 
                           arSA.DateTimeFormat.Calendar.MinSupportedDateTime,  
                           arSA.DateTimeFormat.Calendar.MaxSupportedDateTime); 
      }
   }
}
// The example displays the following output: 
//    9/10/1890 is earlier than 4/30/1900 or later than 5/13/2029

The following example uses each of the standard date time format strings to display the string representation of a date and time for four different cultures.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      // Create an array of all supported standard date and time format specifiers. 
      string[] formats = {"d", "D", "f", "F", "g", "G", "m", "o", "r", 
                          "s", "t", "T", "u", "U", "Y"};
      // Create an array of four cultures.                                 
      CultureInfo[] cultures = {CultureInfo.CreateSpecificCulture("de-DE"), 
                                CultureInfo.CreateSpecificCulture("en-US"), 
                                CultureInfo.CreateSpecificCulture("es-ES"), 
                                CultureInfo.CreateSpecificCulture("fr-FR")};
       // Define date to be displayed.
      DateTime dateToDisplay = new DateTime(2008, 10, 1, 17, 4, 32);

      // Iterate each standard format specifier. 
      foreach (string formatSpecifier in formats)
      {
         foreach (CultureInfo culture in cultures)
            Console.WriteLine("{0} Format Specifier {1, 10} Culture {2, 40}", 
                              formatSpecifier, culture.Name, 
                              dateToDisplay.ToString(formatSpecifier, culture));
         Console.WriteLine();
      }   
   }
}
// The example displays the following output: 
//    d Format Specifier      de-DE Culture                               01.10.2008 
//    d Format Specifier      en-US Culture                                10/1/2008 
//    d Format Specifier      es-ES Culture                               01/10/2008 
//    d Format Specifier      fr-FR Culture                               01/10/2008 
//     
//    D Format Specifier      de-DE Culture                Mittwoch, 1. Oktober 2008 
//    D Format Specifier      en-US Culture              Wednesday, October 01, 2008 
//    D Format Specifier      es-ES Culture         miércoles, 01 de octubre de 2008 
//    D Format Specifier      fr-FR Culture                  mercredi 1 octobre 2008 
//     
//    f Format Specifier      de-DE Culture          Mittwoch, 1. Oktober 2008 17:04 
//    f Format Specifier      en-US Culture      Wednesday, October 01, 2008 5:04 PM 
//    f Format Specifier      es-ES Culture   miércoles, 01 de octubre de 2008 17:04 
//    f Format Specifier      fr-FR Culture            mercredi 1 octobre 2008 17:04 
//     
//    F Format Specifier      de-DE Culture       Mittwoch, 1. Oktober 2008 17:04:32 
//    F Format Specifier      en-US Culture   Wednesday, October 01, 2008 5:04:32 PM 
//    F Format Specifier      es-ES Culture miércoles, 01 de octubre de 2008 17:04:3 
//    F Format Specifier      fr-FR Culture         mercredi 1 octobre 2008 17:04:32 
//     
//    g Format Specifier      de-DE Culture                         01.10.2008 17:04 
//    g Format Specifier      en-US Culture                        10/1/2008 5:04 PM 
//    g Format Specifier      es-ES Culture                         01/10/2008 17:04 
//    g Format Specifier      fr-FR Culture                         01/10/2008 17:04 
//     
//    G Format Specifier      de-DE Culture                      01.10.2008 17:04:32 
//    G Format Specifier      en-US Culture                     10/1/2008 5:04:32 PM 
//    G Format Specifier      es-ES Culture                      01/10/2008 17:04:32 
//    G Format Specifier      fr-FR Culture                      01/10/2008 17:04:32 
//     
//    m Format Specifier      de-DE Culture                               01 Oktober 
//    m Format Specifier      en-US Culture                               October 01 
//    m Format Specifier      es-ES Culture                               01 octubre 
//    m Format Specifier      fr-FR Culture                                1 octobre 
//     
//    o Format Specifier      de-DE Culture              2008-10-01T17:04:32.0000000 
//    o Format Specifier      en-US Culture              2008-10-01T17:04:32.0000000 
//    o Format Specifier      es-ES Culture              2008-10-01T17:04:32.0000000 
//    o Format Specifier      fr-FR Culture              2008-10-01T17:04:32.0000000 
//     
//    r Format Specifier      de-DE Culture            Wed, 01 Oct 2008 17:04:32 GMT 
//    r Format Specifier      en-US Culture            Wed, 01 Oct 2008 17:04:32 GMT 
//    r Format Specifier      es-ES Culture            Wed, 01 Oct 2008 17:04:32 GMT 
//    r Format Specifier      fr-FR Culture            Wed, 01 Oct 2008 17:04:32 GMT 
//     
//    s Format Specifier      de-DE Culture                      2008-10-01T17:04:32 
//    s Format Specifier      en-US Culture                      2008-10-01T17:04:32 
//    s Format Specifier      es-ES Culture                      2008-10-01T17:04:32 
//    s Format Specifier      fr-FR Culture                      2008-10-01T17:04:32 
//     
//    t Format Specifier      de-DE Culture                                    17:04 
//    t Format Specifier      en-US Culture                                  5:04 PM 
//    t Format Specifier      es-ES Culture                                    17:04 
//    t Format Specifier      fr-FR Culture                                    17:04 
//     
//    T Format Specifier      de-DE Culture                                 17:04:32 
//    T Format Specifier      en-US Culture                               5:04:32 PM 
//    T Format Specifier      es-ES Culture                                 17:04:32 
//    T Format Specifier      fr-FR Culture                                 17:04:32 
//     
//    u Format Specifier      de-DE Culture                     2008-10-01 17:04:32Z 
//    u Format Specifier      en-US Culture                     2008-10-01 17:04:32Z 
//    u Format Specifier      es-ES Culture                     2008-10-01 17:04:32Z 
//    u Format Specifier      fr-FR Culture                     2008-10-01 17:04:32Z 
//     
//    U Format Specifier      de-DE Culture     Donnerstag, 2. Oktober 2008 00:04:32 
//    U Format Specifier      en-US Culture   Thursday, October 02, 2008 12:04:32 AM 
//    U Format Specifier      es-ES Culture    jueves, 02 de octubre de 2008 0:04:32 
//    U Format Specifier      fr-FR Culture            jeudi 2 octobre 2008 00:04:32 
//     
//    Y Format Specifier      de-DE Culture                             Oktober 2008 
//    Y Format Specifier      en-US Culture                            October, 2008 
//    Y Format Specifier      es-ES Culture                          octubre de 2008 
//    Y Format Specifier      fr-FR Culture                             octobre 2008

The following example demonstrates different ways of formatting a DateTime value using the invariant DateTimeFormatInfo.

 using System;
 using System.Globalization; 

public class MainClass { 
    public static void Main(string[] args)  {
        DateTime dt = DateTime.Now;
        String[] format = {
            "d", "D",
            "f", "F",
            "g", "G",
            "m",
            "r",
            "s",
            "t", "T",
            "u", "U",
            "y",
            "dddd, MMMM dd yyyy",
            "ddd, MMM d \"'\"yy",
            "dddd, MMMM dd",
            "M/yy",
            "dd-MM-yy",
        };
        String date;
        for (int i = 0; i < format.Length; i++) {
            date = dt.ToString(format[i], DateTimeFormatInfo.InvariantInfo);
            Console.WriteLine(String.Concat(format[i], " :" , date));
        }

   /** Output.
    *
    * d :08/17/2000
    * D :Thursday, August 17, 2000
    * f :Thursday, August 17, 2000 16:32
    * F :Thursday, August 17, 2000 16:32:32
    * g :08/17/2000 16:32
    * G :08/17/2000 16:32:32
    * m :August 17
    * r :Thu, 17 Aug 2000 23:32:32 GMT
    * s :2000-08-17T16:32:32
    * t :16:32
    * T :16:32:32
    * u :2000-08-17 23:32:32Z
    * U :Thursday, August 17, 2000 23:32:32
    * y :August, 2000
    * dddd, MMMM dd yyyy :Thursday, August 17 2000
    * ddd, MMM d "'"yy :Thu, Aug 17 '00
    * dddd, MMMM dd :Thursday, August 17
    * M/yy :8/00
    * dd-MM-yy :17-08-00
    */
    }
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

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

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft