Partager via


DateTime.ToString Method (String)

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 using the specified format.

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

Syntax

'Declaration
Public Function ToString ( _
    format As String _
) As String
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 the format parameter.

Exceptions

Exception Condition
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.

Remarks

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 nulla null reference (Nothing in Visual Basic) or an empty string, the general format specifier, 'G', is used.

This method uses formatting information derived from the current culture. For more information, see CurrentCulture.

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(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).

Imports System.Globalization
Imports System.Threading

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim date1 As Date = #7/21/1550#
      Dim dft As CultureInfo
      Dim heIL As 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
         outputBlock.Text &= date1.ToString("G") & vbCrLf
      Catch e As ArgumentOutOfRangeException
         outputBlock.Text += String.Format("{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)) & vbCrLf
      End Try

      ' Restore the default culture.
      Thread.CurrentThread.CurrentCulture = dft
   End Sub
End Module
' The example displays the following output:
'    07/21/1550 is earlier than 01/01/1583 or later than 09/29/2239
using System;
using System.Globalization;
using System.Threading;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      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
      {
         outputBlock.Text += date1.ToString("G") + "\n";
      }
      catch (ArgumentOutOfRangeException)
      {
         outputBlock.Text += String.Format("{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)) + "\n";
      }

      // 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

Examples

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.

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim dateValue As Date = #6/15/2008 9:15:07 PM#
      ' Create an array of standard format strings.
      Dim standardFmts() As String = {"d", "D", "f", "F", "g", "G", _
                                      "m", "o", "R", "s", "t", "T", _
                                      "u", "U", "y"}
      ' Output date and time using each standard format string.
      For Each standardFmt As String In standardFmts
         outputBlock.Text += String.Format("{0}: {1}", standardFmt, _
                           dateValue.ToString(standardFmt)) & vbCrLf
      Next
      outputBlock.Text &= vbCrLf

      ' Create an array of some custom format strings.
      Dim customFmts() As String = {"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.
      For Each customFmt As String In customFmts
         outputBlock.Text += String.Format("'{0}': {1}", customFmt, _
                           dateValue.ToString(customFmt)) & vbCrLf
      Next
   End Sub
End Module
' This example displays the following output:
'       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
using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      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)
         outputBlock.Text += String.Format("{0}: {1}", standardFmt,
                           dateValue.ToString(standardFmt)) + "\n";
      outputBlock.Text += "\n";

      // 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)
         outputBlock.Text += String.Format("'{0}': {1}", customFmt,
                           dateValue.ToString(customFmt)) + "\n";
   }
}
// This example displays the following output:
//       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

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.