Export (0) Print
Expand All

Convert.ToString Method (DateTime, IFormatProvider)

.NET Framework 1.1

Converts the value of the specified DateTime to its equivalent String representation.

[Visual Basic]
Overloads Public Shared Function ToString( _
   ByVal value As DateTime, _
   ByVal provider As IFormatProvider _
) As String
[C#]
public static string ToString(
 DateTime value,
 IFormatProvider provider
);
[C++]
public: static String* ToString(
 DateTime value,
 IFormatProvider* provider
);
[JScript]
public static function ToString(
   value : DateTime,
 provider : IFormatProvider
) : String;

Parameters

value
A DateTime.
provider
An IFormatProvider interface implementation that supplies culture-specific formatting information.

Return Value

The String equivalent of the value of value.

Remarks

This implementation is identical to DateTime.ToString.

Example

[Visual Basic, C#, C++] The following code example converts a DateTime value to a String with the ToString method, using an IFormatProvider object.

[Visual Basic] 
' Example of the Convert.ToString( DateTime ) and 
' Convert.ToString( DateTime, IFormatProvider ) methods.
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic

Module DateTimeIFormatProviderDemo
    
    Sub DisplayDateNCultureName( testDate As DateTime, _
        cultureName as String )

        ' Create the CultureInfo object for the specified culture,
        ' and use it as the IFormatProvider when converting the date.
        Dim culture  As CultureInfo = new CultureInfo( cultureName )
        Dim dateString As String = _
            Convert.ToString( testDate, culture )

        ' Bracket the culture name, and display the name and date.
        Console.WriteLine( "   {0,-12}{1}", _
            String.Concat( "[", cultureName, "]" ), dateString )
    End Sub

    Sub Main( )
        ' Specify the date to be formatted under various cultures.
        Dim tDate As DateTime = _
            new DateTime( 2003, 4, 15, 20, 30, 40, 333 )

        Console.WriteLine( "This example of " & vbCrLf & _
            "   Convert.ToString( DateTime ) and " & vbCrLf & _
            "   Convert.ToString( DateTime, IFormatProvider )" & _
            vbCrLf & "generates the following output. It " & _
            "creates CultureInfo objects " & vbCrLf & "for " & _
            "several cultures and formats " & _
            "a DateTime value with each." & vbCrLf )

        ' Format the date without an IFormatProvider.
        Console.WriteLine( "   {0,-12}{1}", _
            Nothing, "No IFormatProvider" )
        Console.WriteLine( "   {0,-12}{1}", _
            Nothing, "------------------" )
        Console.WriteLine( "   {0,-12}{1}" & vbCrLf, _
            String.Concat( _
                "[", CultureInfo.CurrentCulture.Name, "]" ), _
            Convert.ToString( tDate ) )

        ' Format the date with IFormatProvider for several cultures.
        Console.WriteLine( "   {0,-12}{1}", _
            "Culture", "With IFormatProvider" )
        Console.WriteLine( "   {0,-12}{1}", _
            "-------", "--------------------" )
        
        DisplayDateNCultureName( tDate, "" )
        DisplayDateNCultureName( tDate, "en-US" )
        DisplayDateNCultureName( tDate, "es-AR" )
        DisplayDateNCultureName( tDate, "fr-FR" )
        DisplayDateNCultureName( tDate, "hi-IN" )
        DisplayDateNCultureName( tDate, "ja-JP" )
        DisplayDateNCultureName( tDate, "nl-NL" )
        DisplayDateNCultureName( tDate, "ru-RU" )
        DisplayDateNCultureName( tDate, "ur-PK" )
    End Sub 
End Module 

' This example of
'    Convert.ToString( DateTime ) and
'    Convert.ToString( DateTime, IFormatProvider )
' generates the following output. It creates CultureInfo objects
' for several cultures and formats a DateTime value with each.
' 
'                No IFormatProvider
'                ------------------
'    [en-US]     4/15/2003 8:30:40 PM
' 
'    Culture     With IFormatProvider
'    -------     --------------------
'    []          04/15/2003 20:30:40
'    [en-US]     4/15/2003 8:30:40 PM
'    [es-AR]     15/04/2003 08:30:40 p.m.
'    [fr-FR]     15/04/2003 20:30:40
'    [hi-IN]     15-04-2003 20:30:40
'    [ja-JP]     2003/04/15 20:30:40
'    [nl-NL]     15-4-2003 20:30:40
'    [ru-RU]     15.04.2003 20:30:40
'    [ur-PK]     15/04/2003 8:30:40 PM

[C#] 
// Example of the Convert.ToString( DateTime ) and 
// Convert.ToString( DateTime, IFormatProvider ) methods.
using System;
using System.Globalization;

class DateTimeIFormatProviderDemo
{
    static void DisplayDateNCultureName( DateTime testDate, 
        string cultureName )
    {
        // Create the CultureInfo object for the specified culture,
        // and use it as the IFormatProvider when converting the date.
        CultureInfo culture = new CultureInfo( cultureName );
        string      dateString = Convert.ToString( testDate, culture );

        // Bracket the culture name, and display the name and date.
        Console.WriteLine("   {0,-12}{1}", 
            String.Concat( "[", cultureName, "]" ), dateString );
    }

    static void Main( )
    {
        // Specify the date to be formatted under various cultures.
        DateTime tDate = new DateTime( 2003, 4, 15, 20, 30, 40, 333 );

        Console.WriteLine( "This example of \n" +
            "   Convert.ToString( DateTime ) and \n" +
            "   Convert.ToString( DateTime, IFormatProvider )\n" +
            "generates the following output. It creates " +
            "CultureInfo objects \nfor several cultures " +
            "and formats a DateTime value with each.\n" );

        // Format the date without an IFormatProvider.
        Console.WriteLine( "   {0,-12}{1}", 
            null, "No IFormatProvider" );
        Console.WriteLine( "   {0,-12}{1}", 
            null, "------------------" );
        Console.WriteLine( "   {0,-12}{1}\n", 
            String.Concat( "[", CultureInfo.CurrentCulture.Name, "]" ), 
            Convert.ToString( tDate ) );

        // Format the date with IFormatProvider for several cultures.
        Console.WriteLine( "   {0,-12}{1}", 
            "Culture", "With IFormatProvider" );
        Console.WriteLine( "   {0,-12}{1}", 
            "-------", "--------------------" );
        
        DisplayDateNCultureName( tDate, "" );
        DisplayDateNCultureName( tDate, "en-US" );
        DisplayDateNCultureName( tDate, "es-AR" );
        DisplayDateNCultureName( tDate, "fr-FR" );
        DisplayDateNCultureName( tDate, "hi-IN" );
        DisplayDateNCultureName( tDate, "ja-JP" );
        DisplayDateNCultureName( tDate, "nl-NL" );
        DisplayDateNCultureName( tDate, "ru-RU" );
        DisplayDateNCultureName( tDate, "ur-PK" );
    }
}

/*
This example of
   Convert.ToString( DateTime ) and
   Convert.ToString( DateTime, IFormatProvider )
generates the following output. It creates CultureInfo objects
for several cultures and formats a DateTime value with each.

               No IFormatProvider
               ------------------
   [en-US]     4/15/2003 8:30:40 PM

   Culture     With IFormatProvider
   -------     --------------------
   []          04/15/2003 20:30:40
   [en-US]     4/15/2003 8:30:40 PM
   [es-AR]     15/04/2003 08:30:40 p.m.
   [fr-FR]     15/04/2003 20:30:40
   [hi-IN]     15-04-2003 20:30:40
   [ja-JP]     2003/04/15 20:30:40
   [nl-NL]     15-4-2003 20:30:40
   [ru-RU]     15.04.2003 20:30:40
   [ur-PK]     15/04/2003 8:30:40 PM
*/ 

[C++] 
// Example of the Convert::ToString( DateTime ) and 
// Convert::ToString( DateTime, IFormatProvider* ) methods.
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;

#define null (Object*)0

void DisplayDateNCultureName( DateTime testDate, String* cultureName )
{
    // Create the CultureInfo object for the specified culture,
    // and use it as the IFormatProvider when converting the date.
    CultureInfo* culture = new CultureInfo( cultureName );
    String*      dateString = Convert::ToString( testDate, culture );

    // Bracket the culture name, and display the name and date.
    Console::WriteLine(S"   {0,-12}{1}", 
        String::Concat( S"[", cultureName, S"]" ), dateString );
}

void main( )
{
    // Specify the date to be formatted under various cultures.
    DateTime tDate = DateTime( 2003, 4, 15, 20, 30, 40, 333 );

    Console::WriteLine( S"This example of \n" 
        S"   Convert::ToString( DateTime ) and \n" 
        S"   Convert::ToString( DateTime, IFormatProvider* )\n" 
        S"generates the following output. It creates " 
        S"CultureInfo objects \nfor several cultures " 
        S"and formats a DateTime value with each.\n" );

    // Format the date without an IFormatProvider.
    Console::WriteLine( S"   {0,-12}{1}", 
        null, S"No IFormatProvider" );
    Console::WriteLine( S"   {0,-12}{1}", 
        null, S"------------------" );
    Console::WriteLine( S"   {0,-12}{1}\n", 
        String::Concat( S"[", CultureInfo::CurrentCulture->Name, S"]" ), 
        Convert::ToString( tDate ) );

    // Format the date with IFormatProvider for several cultures.
    Console::WriteLine( S"   {0,-12}{1}", 
        S"Culture", S"With IFormatProvider" );
    Console::WriteLine( S"   {0,-12}{1}", 
        S"-------", S"--------------------" );
    
    DisplayDateNCultureName( tDate, S"" );
    DisplayDateNCultureName( tDate, S"en-US" );
    DisplayDateNCultureName( tDate, S"es-AR" );
    DisplayDateNCultureName( tDate, S"fr-FR" );
    DisplayDateNCultureName( tDate, S"hi-IN" );
    DisplayDateNCultureName( tDate, S"ja-JP" );
    DisplayDateNCultureName( tDate, S"nl-NL" );
    DisplayDateNCultureName( tDate, S"ru-RU" );
    DisplayDateNCultureName( tDate, S"ur-PK" );
}

/*
This example of
   Convert::ToString( DateTime ) and
   Convert::ToString( DateTime, IFormatProvider* )
generates the following output. It creates CultureInfo objects
for several cultures and formats a DateTime value with each.

               No IFormatProvider
               ------------------
   [en-US]     4/15/2003 8:30:40 PM

   Culture     With IFormatProvider
   -------     --------------------
   []          04/15/2003 20:30:40
   [en-US]     4/15/2003 8:30:40 PM
   [es-AR]     15/04/2003 08:30:40 p.m.
   [fr-FR]     15/04/2003 20:30:40
   [hi-IN]     15-04-2003 20:30:40
   [ja-JP]     2003/04/15 20:30:40
   [nl-NL]     15-4-2003 20:30:40
   [ru-RU]     15.04.2003 20:30:40
   [ur-PK]     15/04/2003 8:30:40 PM
*/ 

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

See Also

Convert Class | Convert Members | System Namespace | Convert.ToString Overload List

Show:
© 2015 Microsoft