Partager via


DateTime.ToString Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Converts the value of the current DateTime object to its equivalent string representation.

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

Syntax

'Declaration
Public Overrides Function ToString As String
public override string ToString()

Return Value

Type: System.String
A string representation of the value of the current DateTime object.

Exceptions

Exception Condition
ArgumentOutOfRangeException

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

Remarks

The value of the current DateTime object is formatted using the general date and time format specifier ('G').

This method uses formatting information derived from the current culture. In particular, it combines the custom format strings returned by the ShortDatePattern and LongTimePattern properties of the DateTimeFormatInfo object returned by the Thread.CurrentThread.CurrentCulture.DateTimeFormat property. For more information, see CultureInfo.CurrentCulture. Other overloads of the ToString method enable you to specify the culture whose formatting to use and to define the output pattern of the DateTime value.

Platform Notes

Silverlight for Windows Phone Silverlight for Windows Phone

 Silverlight for Windows Phone does not return the correct string for Russian dates. For example, 15.Июнь.2000 is returned instead of 15.июня.2000.

Notes to Callers

The ToString() 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 HijriCalendar class when the current culture is Arabic (Syria).

Imports System.Globalization
Imports System.Threading

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim date1 As Date = #1/1/0550#
      Dim dft As CultureInfo
      Dim arSY As New CultureInfo("ar-SY")
      arSY.DateTimeFormat.Calendar = New HijriCalendar()

      ' Change current culture to arSY.
      dft = Thread.CurrentThread.CurrentCulture
      Thread.CurrentThread.CurrentCulture = arSY

      ' Display the date using the current culture's calendar.            
      Try
         outputBlock.Text &= date1.ToString() & vbCrLf
      Catch e As ArgumentOutOfRangeException
         outputBlock.Text += String.Format("{0} is earlier than {1} or later than {2}", _
                           date1.ToString("d", CultureInfo.InvariantCulture), _
                           arSY.DateTimeFormat.Calendar.MinSupportedDateTime.ToString("d", CultureInfo.InvariantCulture), _
                           arSY.DateTimeFormat.Calendar.MaxSupportedDateTime.ToString("d", CultureInfo.InvariantCulture)) & vbCrLf
      End Try

      ' Restore the default culture.
      Thread.CurrentThread.CurrentCulture = dft
   End Sub
End Module
' The example displays the following output:
'    01/01/0550 is earlier than 07/18/0622 or later than 12/31/9999
using System;
using System.Globalization;
using System.Threading;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      DateTime date1 = new DateTime(550, 1, 1);
      CultureInfo dft;
      CultureInfo arSY = new CultureInfo("ar-SY");
      arSY.DateTimeFormat.Calendar = new HijriCalendar();

      // Change current culture to arSY.
      dft = Thread.CurrentThread.CurrentCulture;
      Thread.CurrentThread.CurrentCulture = arSY;

      // Display the date using the current culture's calendar.            
      try
      {
         outputBlock.Text += date1.ToString() + "\n";
      }
      catch (ArgumentOutOfRangeException)
      {
         outputBlock.Text += String.Format("{0} is earlier than {1} or later than {2}",
                           date1.ToString("d", CultureInfo.InvariantCulture),
                           arSY.DateTimeFormat.Calendar.MinSupportedDateTime.ToString("d", CultureInfo.InvariantCulture),
                           arSY.DateTimeFormat.Calendar.MaxSupportedDateTime.ToString("d", CultureInfo.InvariantCulture)) + "\n";
      }

      // Restore the default culture.
      Thread.CurrentThread.CurrentCulture = dft;
   }
}
// The example displays the following output:
//    01/01/0550 is earlier than 07/18/0622 or later than 12/31/9999

Examples

The following example illustrates how the string representation of a DateTime value returned by the ToString() method depends on the thread current culture. It changes the current thread culture from en-US to fr-FR to ja-JP. and in each case calls the ToString() method to return the string representation of a date and time value using that culture.

Imports System.Globalization
Imports System.Threading

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim currentCulture As CultureInfo = Thread.CurrentThread.CurrentCulture
      Dim exampleDate As Date = #5/1/2008 6:32:06 PM#

      ' Display the date using the current (en-US) culture.
      outputBlock.Text &= exampleDate.ToString() & vbCrLf

      ' Change the current culture to fr-FR and display the date.
      Thread.CurrentThread.CurrentCulture = New CultureInfo("fr-FR")
      outputBlock.Text &= exampleDate.ToString() & vbCrLf

      ' Change the current culture to ja-JP and display the date.
      Thread.CurrentThread.CurrentCulture = New CultureInfo("ja-JP")
      outputBlock.Text &= exampleDate.ToString() & vbCrLf

      ' Restore the original culture
      Thread.CurrentThread.CurrentCulture = currentCulture
   End Sub
End Module
' The example displays the following output:
'       5/1/2008 6:32:06 PM
'       01/05/2008 18:32:06
'       2008/05/01 18:32:06
using System;
using System.Globalization;
using System.Threading;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
      DateTime exampleDate = new DateTime(2008, 5, 1, 18, 32, 6);

      // Display the date using the current (en-US) culture.
      outputBlock.Text += exampleDate.ToString() + "\n";

      // Change the current culture to fr-FR and display the date.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
      outputBlock.Text += exampleDate.ToString() + "\n";

      // Change the current culture to ja-JP and display the date.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("ja-JP");
      outputBlock.Text += exampleDate.ToString() + "\n";

      // Restore the original culture
      Thread.CurrentThread.CurrentCulture = currentCulture;
   }
}
// The example displays the following output:
//       5/1/2008 6:32:06 PM
//       01/05/2008 18:32:06
//       2008/05/01 18:32:06

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.