次の方法で共有


Convert.ToString メソッド (DateTime)

指定した DateTime の値を、それと等価な String 形式に変換します。

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

パラメータ

戻り値

value の値と等価な String

解説

この実装は、 DateTime.ToString と同じです。

使用例

[Visual Basic, C#, C++] 既定の書式設定を使用し、 ToString メソッドで DateTime の値を String に変換するコード例を次に示します。

 
' 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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

Convert クラス | Convert メンバ | System 名前空間 | Convert.ToString オーバーロードの一覧