Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
System Namespace
DateTime Structure
DateTime Methods
ToString Method
 ToString Method
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
DateTime..::.ToString Method

Updated: November 2007

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
Visual Basic (Usage)
Dim instance As DateTime
Dim returnValue As String

returnValue = instance.ToString()
C#
public override string ToString()
Visual C++
public:
virtual String^ ToString() override
J#
public String ToString()
JScript
public override function ToString() : String

Return Value

Type: System..::.String

A string representation of the value of the current DateTime object.

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.

Visual Basic
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

C#
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 Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, 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

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: 2.0, 1.0
Community Content   What is Community Content?
Add new content RSS  Annotations
Can throw ArgumentOutOfRangeException when using the JapaneseCalender      David M. Kean - MSFT   |   Edit   |  
This method throws an ArgumentOutOfRangeException when using the JapaneseCalendar to format a date that occurs before September 8th, 1868.

The following method attempts to call DateTime.ToString(IFormatProvider) on a DateTime instance with the value September 7th, 1868.

[C#]

using System;
using System.Globalization;

namespace Samples
{
class Program
{
static void Main(string[] args)
{
CultureInfo culture = new CultureInfo("ja-JP");
culture.DateTimeFormat.Calendar = new JapaneseCalendar();

DateTime date = new DateTime(1868, 9, 7);


try
{
date.ToString(culture);
}
catch (ArgumentOutOfRangeException ex)
{
Console.WriteLine(ex.Message);
}
}
}
}

The above outputs the following:

Specified time is not supported in this calendar. It should be between 09/08/186
8 00:00:00 (Gregorian date) and 12/31/9999 23:59:59 (Gregorian date), inclusive.


Parameter name: time


This can also occur without explicitly specifying the JapaneseCalendar when the current user's calender is set to the Wareki calendar in Regional and Language Options. To set the calendar to this, do the following in Windows Vista and Windows Server 2003:

  1. Choose Start -> Control Panel -> Regional and Language Options
  2. From the Current format drop down, choose Japanese (Japan)
  3. Click Customize this format and select the Date tab
  4. Under Calendar type, choose 和暦
  5. Click OK and then OK again

The following method attempts to call DateTime.ToString() on a DateTime instance with the value September 7th, 1868. This code sample expects the above steps to have been done.

[C#]

using System;
using System.Globalization;

namespace Samples
{
class Program
{
static void Main(string[] args)
{
DateTime date = new DateTime(1868, 9, 7);


try
{
date.ToString();
}
catch (ArgumentOutOfRangeException ex)
{
Console.WriteLine(ex.Message);
}
}
}
}

The above outputs the following:

Specified time is not supported in this calendar. It should be between 09/08/186
8 00:00:00 (Gregorian date) and 12/31/9999 23:59:59 (Gregorian date), inclusive.


Parameter name: time
Tags What's this?: null (x) Add a tag
Flag as ContentBug
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker