Converts the value of the current DateTime object to its equivalent string representation.
Namespace:
System
Assembly:
mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Overrides Function ToString As String
Dim instance As DateTime
Dim returnValue As String
returnValue = instance.ToString()
public override string ToString()
public:
virtual String^ ToString() override
public override function ToString() : String
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.
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 DateToStringExample
Public Sub Main()
Dim currentCulture As CultureInfo = Thread.CurrentThread.CurrentCulture
Dim exampleDate As Date = #05/01/2008 6:32:06PM#
' Display the date using the current (en-US) culture.
Console.WriteLine(exampleDate.ToString())
' Change the current culture to fr-FR and display the date.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR")
Console.WriteLine(exampleDate.ToString())
' Change the current culture to ja-JP and display the date.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("ja-JP")
Console.WriteLine(exampleDate.ToString())
' Restore the original culture
Thread.CurrentThread.CurrentCulture = currentCulture
End Sub
End Module
' The example displays the following output to the console:
' 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 DateToStringExample
{
public static void Main()
{
CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
DateTime exampleDate = new DateTime(2008, 5, 1, 18, 32, 6);
// Display the date using the current (en-US) culture.
Console.WriteLine(exampleDate.ToString());
// Change the current culture to fr-FR and display the date.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR");
Console.WriteLine(exampleDate.ToString());
// Change the current culture to ja-JP and display the date.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("ja-JP");
Console.WriteLine(exampleDate.ToString());
// Restore the original culture
Thread.CurrentThread.CurrentCulture = currentCulture;
}
}
// The example displays the following output to the console:
// 5/1/2008 6:32:06 PM
// 01/05/2008 18:32:06
// 2008/05/01 18:32:06
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
.NET Compact Framework
Supported in: 3.5, 2.0, 1.0
XNA Framework
Supported in: 3.0, 2.0, 1.0
Reference