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 (IFormatProvider)
Collapse All/Expand All Collapse All
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 (IFormatProvider)

Updated: July 2008

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Function ToString ( _
    provider As IFormatProvider _
) As String
Visual Basic (Usage)
Dim instance As DateTime
Dim provider As IFormatProvider
Dim returnValue As String

returnValue = instance.ToString(provider)
C#
public string ToString(
    IFormatProvider provider
)
Visual C++
public:
virtual String^ ToString(
    IFormatProvider^ provider
) sealed
JScript
public final function ToString(
    provider : IFormatProvider
) : String

Parameters

provider
Type: System..::.IFormatProvider
An IFormatProvider that supplies culture-specific formatting information.

Return Value

Type: System..::.String
A string representation of value of the current DateTime object as specified by provider.

Implements

IConvertible..::.ToString(IFormatProvider)

The value of the current DateTime object is formatted using the general date and time format specifier ('G'), which formats output using the short date pattern and the long time pattern.

The format of the short date and long time pattern is defined by the provider parameter. The provider parameter can be any of the following:

If provider is nullNothingnullptra null reference (Nothing in Visual Basic), the DateTimeFormatInfo object associated with the current culture is used. For more information, see CultureInfo..::.CurrentCulture.

The following example displays the string representation of a date and time using CultureInfo objects that represent five different cultures.

Visual Basic
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim cultures() As CultureInfo = {CultureInfo.InvariantCulture, _
                                       New CultureInfo("en-us"), _
                                       New CultureInfo("fr-fr"), _
                                       New CultureInfo("de-DE"), _
                                       New CultureInfo("es-ES"), _
                                       New CultureInfo("ja-JP")}

      Dim thisDate As Date =#5/1/2009 9:00AM#                                            

      For Each culture As CultureInfo In cultures
         Dim cultureName As String 
         If String.IsNullOrEmpty(culture.Name) Then
            cultureName = culture.NativeName
         Else
            cultureName = culture.Name
         End If

         Console.WriteLine("In {0}, {1}", _
                           cultureName, thisDate.ToString(culture))
      Next                                            
   End Sub
End Module
' The example produces the following output:
'    In Invariant Language (Invariant Country), 05/01/2009 09:00:00
'    In en-US, 5/1/2009 9:00:00 AM
'    In fr-FR, 01/05/2009 09:00:00
'    In de-DE, 01.05.2009 09:00:00
'    In es-ES, 01/05/2009 9:00:00
'    In ja-JP, 2009/05/01 9:00:00
C#
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      CultureInfo[] cultures = new CultureInfo[] {CultureInfo.InvariantCulture, 
                                                  new CultureInfo("en-us"), 
                                                  new CultureInfo("fr-fr"), 
                                                  new CultureInfo("de-DE"), 
                                                  new CultureInfo("es-ES"),
                                                  new CultureInfo("ja-JP")};

      DateTime thisDate = new DateTime(2009, 5, 1, 9, 0, 0);                                            

      foreach (CultureInfo culture in cultures)
      {
         string cultureName; 
         if (string.IsNullOrEmpty(culture.Name))
            cultureName = culture.NativeName;
         else
            cultureName = culture.Name;

         Console.WriteLine("In {0}, {1}", 
                           cultureName, thisDate.ToString(culture));
      }                                            
      }
}
// The example produces the following output:
//    In Invariant Language (Invariant Country), 05/01/2009 09:00:00
//    In en-US, 5/1/2009 9:00:00 AM
//    In fr-FR, 01/05/2009 09:00:00
//    In de-DE, 01.05.2009 09:00:00
//    In es-ES, 01/05/2009 9:00:00
//    In ja-JP, 2009/05/01 9:00:00

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

Date

History

Reason

July 2008

Added detail on method behavior; revised the example.

Information enhancement.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Can throw ArgumentOutOfRangeException when using certain calenders      David M. Kean - MSFT   |   Edit   |   Show History

This method throws an ArgumentOutOfRangeException when attempting to format a DateTime whose value is outside the valid range for the current culture's calender. For example, both the JapaneseCalendar and UmAlQuraCalender objects cannot represent every valid value of a DateTime. You can find the range of supported values from the Calendar.MinSupportedDateTime and Calendar.MaxSupportedDateTime properties.

For example, an ArgumentOutOfRangeException will be thrown when using the the JapaneseCalendar to format a date that occurs before September 8th, 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
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker