Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

DateTime.ToString Method (String)

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

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

Public Function ToString ( _
	format As String _
) As String


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.


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


format does not contain a valid custom format pattern.


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

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

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 Main()
      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.             
      Catch e As 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)) 
      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

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 DateToStringExample
   Public Sub Main()
      Dim dateValue As Date = #6/15/2008 9:15:07PM#
      ' 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
         Console.WriteLine("{0}: {1}", standardFmt, _

      ' 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
         Console.WriteLine("'{0}': {1}", customFmt, _
   End Sub 
End Module 
' 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

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
© 2015 Microsoft