Convert.ToDateTime メソッド
指定した値を DateTime に変換します。
オーバーロードの一覧
このメソッドを呼び出すと、必ず InvalidCastException がスローされます。
[Visual Basic] Overloads Public Shared Function ToDateTime(Boolean) As DateTime
[JScript] public static function ToDateTime(Boolean) : DateTime;
このメソッドを呼び出すと、必ず InvalidCastException がスローされます。
[Visual Basic] Overloads Public Shared Function ToDateTime(Byte) As DateTime
[JScript] public static function ToDateTime(Byte) : DateTime;
このメソッドを呼び出すと、必ず InvalidCastException がスローされます。
[Visual Basic] Overloads Public Shared Function ToDateTime(Char) As DateTime
[JScript] public static function ToDateTime(Char) : DateTime;
指定した DateTime を返します。実際の変換処理は実行されません。
[Visual Basic] Overloads Public Shared Function ToDateTime(DateTime) As DateTime
[JScript] public static function ToDateTime(DateTime) : DateTime;
このメソッドを呼び出すと、必ず InvalidCastException がスローされます。
[Visual Basic] Overloads Public Shared Function ToDateTime(Decimal) As DateTime
[JScript] public static function ToDateTime(Decimal) : DateTime;
このメソッドを呼び出すと、必ず InvalidCastException がスローされます。
[Visual Basic] Overloads Public Shared Function ToDateTime(Double) As DateTime
[JScript] public static function ToDateTime(double) : DateTime;
このメソッドを呼び出すと、必ず InvalidCastException がスローされます。
[Visual Basic] Overloads Public Shared Function ToDateTime(Short) As DateTime
[JScript] public static function ToDateTime(Int16) : DateTime;
このメソッドを呼び出すと、必ず InvalidCastException がスローされます。
[Visual Basic] Overloads Public Shared Function ToDateTime(Integer) As DateTime
[JScript] public static function ToDateTime(int) : DateTime;
このメソッドを呼び出すと、必ず InvalidCastException がスローされます。
[Visual Basic] Overloads Public Shared Function ToDateTime(Long) As DateTime
[JScript] public static function ToDateTime(long) : DateTime;
指定した Object の値を DateTime に変換します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function ToDateTime(Object) As DateTime
[JScript] public static function ToDateTime(Object) : DateTime;
このメソッドを呼び出すと、必ず InvalidCastException がスローされます。このメソッドは、CLS と互換性がありません。
[Visual Basic] Overloads Public Shared Function ToDateTime(SByte) As DateTime
[JScript] public static function ToDateTime(SByte) : DateTime;
このメソッドを呼び出すと、必ず InvalidCastException がスローされます。
[Visual Basic] Overloads Public Shared Function ToDateTime(Single) As DateTime
[JScript] public static function ToDateTime(float) : DateTime;
指定した String 形式の日付と時刻を等価の DateTime に変換します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function ToDateTime(String) As DateTime
[JScript] public static function ToDateTime(String) : DateTime;
このメソッドを呼び出すと、必ず InvalidCastException がスローされます。このメソッドは、CLS と互換性がありません。
[Visual Basic] Overloads Public Shared Function ToDateTime(UInt16) As DateTime
[JScript] public static function ToDateTime(UInt16) : DateTime;
このメソッドを呼び出すと、必ず InvalidCastException がスローされます。このメソッドは、CLS と互換性がありません。
[Visual Basic] Overloads Public Shared Function ToDateTime(UInt32) As DateTime
[JScript] public static function ToDateTime(UInt32) : DateTime;
このメソッドを呼び出すと、必ず InvalidCastException がスローされます。このメソッドは、CLS と互換性がありません。
[Visual Basic] Overloads Public Shared Function ToDateTime(UInt64) As DateTime
[JScript] public static function ToDateTime(UInt64) : DateTime;
指定したカルチャに固有の書式情報を使用して指定した Object の値を等価の DateTime 文字に変換します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function ToDateTime(Object, IFormatProvider) As DateTime
[C#] public static DateTime ToDateTime(object, IFormatProvider);
[C++] public: static DateTime ToDateTime(Object*, IFormatProvider*);
[JScript] public static function ToDateTime(Object, IFormatProvider) : DateTime;
指定したカルチャに固有の書式情報を使用して、指定した String 形式の数値を等価の DateTime に変換します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function ToDateTime(String, IFormatProvider) As DateTime
[C#] public static DateTime ToDateTime(string, IFormatProvider);
[C++] public: static DateTime ToDateTime(String*, IFormatProvider*);
[JScript] public static function ToDateTime(String, IFormatProvider) : DateTime;
使用例
[Visual Basic, C#, C++] IFormatProvider オブジェクトを使用し、 String 形式の DateTime の値を ToDateTime メソッドで変換するコード例を次に示します。
[Visual Basic, C#, C++] メモ ここでは、ToDateTime のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。
' Example of Convert.ToDateTime( String, IFormatProvider ).
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic
Module StringToDateTimeDemo
Const lineFmt As String = "{0,-18}{1,-12}{2}"
' Get the exception type name; remove the namespace prefix.
Function GetExceptionType( ex As Exception ) As String
Dim exceptionType As String = ex.GetType( ).ToString( )
Return exceptionType.Substring( _
exceptionType.LastIndexOf( "."c ) + 1 )
End Function
Sub StringToDateTime( cultureName As String )
Dim dateStrings As String( ) = { "01/02/03", _
"2001/02/03", "01/2002/03", "01/02/2003", _
"21/02/03", "01/22/03", "01/02/23" }
Dim culture As CultureInfo = New CultureInfo( cultureName )
Console.WriteLine( )
' Convert each string in the dateStrings array.
Dim dateStr As String
For Each dateStr In dateStrings
Dim dateTimeValue As DateTime
' Display the first part of the output line.
Console.Write( lineFmt, dateStr, cultureName, Nothing )
' Convert the string to a DateTime object.
Try
dateTimeValue = _
Convert.ToDateTime( dateStr, culture )
' Display the DateTime object in a fixed format
' if Convert succeeded.
Console.WriteLine( "{0:yyyy-MMM-dd}", dateTimeValue )
' Display the exception type if Parse failed.
Catch ex As Exception
Console.WriteLine( "{0}", GetExceptionType( ex ) )
End Try
Next dateStr
End Sub
Sub Main( )
Console.WriteLine( "This example of " & _
"Convert.ToDateTime( String, IFormatProvider ) " & _
vbCrLf & "generates the following output. Several " & _
"strings are converted " & vbCrLf & "to DateTime " & _
"objects using formatting information from different " & _
vbCrLf & "cultures, and then the strings are " & _
"displayed in a " & vbCrLf & "culture-invariant form." & _
vbCrLf )
Console.WriteLine( lineFmt, "Date String", "Culture", _
"DateTime or Exception" )
Console.WriteLine( lineFmt, "-----------", "-------", _
"---------------------" )
StringToDateTime( "en-US" )
StringToDateTime( "ru-RU" )
StringToDateTime( "ja-JP" )
End Sub
End Module
' This example of Convert.ToDateTime( String, IFormatProvider )
' generates the following output. Several strings are converted
' to DateTime objects using formatting information from different
' cultures, and then the strings are displayed in a
' culture-invariant form.
'
' Date String Culture DateTime or Exception
' ----------- ------- ---------------------
'
' 01/02/03 en-US 2003-Jan-02
' 2001/02/03 en-US 2001-Feb-03
' 01/2002/03 en-US 2002-Jan-03
' 01/02/2003 en-US 2003-Jan-02
' 21/02/03 en-US FormatException
' 01/22/03 en-US 2003-Jan-22
' 01/02/23 en-US 2023-Jan-02
'
' 01/02/03 ru-RU 2003-Feb-01
' 2001/02/03 ru-RU 2001-Feb-03
' 01/2002/03 ru-RU 2002-Jan-03
' 01/02/2003 ru-RU 2003-Feb-01
' 21/02/03 ru-RU 2003-Feb-21
' 01/22/03 ru-RU FormatException
' 01/02/23 ru-RU 2023-Feb-01
'
' 01/02/03 ja-JP 2001-Feb-03
' 2001/02/03 ja-JP 2001-Feb-03
' 01/2002/03 ja-JP 2002-Jan-03
' 01/02/2003 ja-JP 2003-Jan-02
' 21/02/03 ja-JP 2021-Feb-03
' 01/22/03 ja-JP FormatException
' 01/02/23 ja-JP 2001-Feb-23
[C#]
// Example of Convert.ToDateTime( String, IFormatProvider ).
using System;
using System.Globalization;
class StringToDateTimeDemo
{
const string lineFmt = "{0,-18}{1,-12}{2}";
// Get the exception type name; remove the namespace prefix.
public static string GetExceptionType( Exception ex )
{
string exceptionType = ex.GetType( ).ToString( );
return exceptionType.Substring(
exceptionType.LastIndexOf( '.' ) + 1 );
}
public static void StringToDateTime( string cultureName )
{
string[ ] dateStrings = { "01/02/03",
"2001/02/03", "01/2002/03", "01/02/2003",
"21/02/03", "01/22/03", "01/02/23" };
CultureInfo culture = new CultureInfo( cultureName );
Console.WriteLine( );
// Convert each string in the dateStrings array.
foreach( string dateStr in dateStrings )
{
DateTime dateTimeValue;
// Display the first part of the output line.
Console.Write( lineFmt, dateStr, cultureName, null );
try
{
// Convert the string to a DateTime object.
dateTimeValue = Convert.ToDateTime( dateStr, culture );
// Display the DateTime object in a fixed format
// if Convert succeeded.
Console.WriteLine( "{0:yyyy-MMM-dd}", dateTimeValue );
}
catch( Exception ex )
{
// Display the exception type if Parse failed.
Console.WriteLine( "{0}", GetExceptionType( ex ) );
}
}
}
public static void Main( )
{
Console.WriteLine( "This example of " +
"Convert.ToDateTime( String, IFormatProvider ) " +
"\ngenerates the following output. Several strings are " +
"converted \nto DateTime objects using formatting " +
"information from different \ncultures, and then the " +
"strings are displayed in a \nculture-invariant form.\n" );
Console.WriteLine( lineFmt, "Date String", "Culture",
"DateTime or Exception" );
Console.WriteLine( lineFmt, "-----------", "-------",
"---------------------" );
StringToDateTime( "en-US" );
StringToDateTime( "ru-RU" );
StringToDateTime( "ja-JP" );
}
}
/*
This example of Convert.ToDateTime( String, IFormatProvider )
generates the following output. Several strings are converted
to DateTime objects using formatting information from different
cultures, and then the strings are displayed in a
culture-invariant form.
Date String Culture DateTime or Exception
----------- ------- ---------------------
01/02/03 en-US 2003-Jan-02
2001/02/03 en-US 2001-Feb-03
01/2002/03 en-US 2002-Jan-03
01/02/2003 en-US 2003-Jan-02
21/02/03 en-US FormatException
01/22/03 en-US 2003-Jan-22
01/02/23 en-US 2023-Jan-02
01/02/03 ru-RU 2003-Feb-01
2001/02/03 ru-RU 2001-Feb-03
01/2002/03 ru-RU 2002-Jan-03
01/02/2003 ru-RU 2003-Feb-01
21/02/03 ru-RU 2003-Feb-21
01/22/03 ru-RU FormatException
01/02/23 ru-RU 2023-Feb-01
01/02/03 ja-JP 2001-Feb-03
2001/02/03 ja-JP 2001-Feb-03
01/2002/03 ja-JP 2002-Jan-03
01/02/2003 ja-JP 2003-Jan-02
21/02/03 ja-JP 2021-Feb-03
01/22/03 ja-JP FormatException
01/02/23 ja-JP 2001-Feb-23
*/
[C++]
// Example of Convert::ToDateTime( String*, IFormatProvider* ).
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;
using namespace System::Collections;
const __wchar_t* protoFmt = L"{0,-18}{1,-12}{2}" ;
// Get the exception type name; remove the namespace prefix.
String* GetExceptionType( Exception* ex )
{
String* exceptionType = ex->GetType( )->ToString( );
return exceptionType->Substring(
exceptionType->LastIndexOf( '.' ) + 1 );
}
// Convert each string in the dateStrings array.
void StringToDateTime( String* cultureName )
{
String* dateStrings[ ] = __gc new String*[ 7 ];
dateStrings[ 0 ] = S"01/02/03";
dateStrings[ 1 ] = S"2001/02/03";
dateStrings[ 2 ] = S"01/2002/03";
dateStrings[ 3 ] = S"01/02/2003";
dateStrings[ 4 ] = S"21/02/03";
dateStrings[ 5 ] = S"01/22/03";
dateStrings[ 6 ] = S"01/02/23";
CultureInfo* culture = new CultureInfo( cultureName );
Console::WriteLine( );
// Code foreach( String* dateStr in dateStrings ).
IEnumerator* myEnum = dateStrings->GetEnumerator( );
while( myEnum->MoveNext( ) )
{
String* dateStr =
__try_cast<String*>( myEnum->Current );
DateTime dateTimeValue;
// Display the first part of the output line.
Console::Write( new String( protoFmt ), dateStr,
cultureName, S"" );
try
{
// Convert the string to a DateTime object.
dateTimeValue = Convert::ToDateTime( dateStr, culture );
// Display the DateTime object in a fixed format
// if Convert succeeded.
Console::WriteLine( S"{0:yyyy-MMM-dd}",
__box( dateTimeValue ) );
}
catch( Exception* ex )
{
// Display the exception type if Parse failed.
Console::WriteLine( S"{0}", GetExceptionType( ex ) );
}
}
}
void main( )
{
Console::WriteLine( S"This example of "
S"Convert::ToDateTime( String*, IFormatProvider* ) "
S"\ngenerates the following output. Several strings are "
S"converted \nto DateTime objects using formatting "
S"information from different \ncultures, and then the "
S"strings are displayed in a \nculture-invariant form.\n" );
Console::WriteLine( new String( protoFmt ), S"Date String",
S"Culture", S"DateTime or Exception" );
Console::WriteLine( new String( protoFmt ), S"-----------",
S"-------", S"---------------------" );
StringToDateTime( S"en-US" );
StringToDateTime( S"ru-RU" );
StringToDateTime( S"ja-JP" );
}
/*
This example of Convert::ToDateTime( String*, IFormatProvider* )
generates the following output. Several strings are converted
to DateTime objects using formatting information from different
cultures, and then the strings are displayed in a
culture-invariant form.
Date String Culture DateTime or Exception
----------- ------- ---------------------
01/02/03 en-US 2003-Jan-02
2001/02/03 en-US 2001-Feb-03
01/2002/03 en-US 2002-Jan-03
01/02/2003 en-US 2003-Jan-02
21/02/03 en-US FormatException
01/22/03 en-US 2003-Jan-22
01/02/23 en-US 2023-Jan-02
01/02/03 ru-RU 2003-Feb-01
2001/02/03 ru-RU 2001-Feb-03
01/2002/03 ru-RU 2002-Jan-03
01/02/2003 ru-RU 2003-Feb-01
21/02/03 ru-RU 2003-Feb-21
01/22/03 ru-RU FormatException
01/02/23 ru-RU 2023-Feb-01
01/02/03 ja-JP 2001-Feb-03
2001/02/03 ja-JP 2001-Feb-03
01/2002/03 ja-JP 2002-Jan-03
01/02/2003 ja-JP 2003-Jan-02
21/02/03 ja-JP 2021-Feb-03
01/22/03 ja-JP FormatException
01/02/23 ja-JP 2001-Feb-23
*/
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。