Export (0) Print
Expand All

Convert.ToDateTime Method

Converts a specified value to a DateTime.

Overload List

Calling this method always throws InvalidCastException.

[Visual Basic] Overloads Public Shared Function ToDateTime(Boolean) As DateTime
[C#] public static DateTime ToDateTime(bool);
[C++] public: static DateTime ToDateTime(bool);
[JScript] public static function ToDateTime(Boolean) : DateTime;

Calling this method always throws InvalidCastException.

[Visual Basic] Overloads Public Shared Function ToDateTime(Byte) As DateTime
[C#] public static DateTime ToDateTime(byte);
[C++] public: static DateTime ToDateTime(unsigned char);
[JScript] public static function ToDateTime(Byte) : DateTime;

Calling this method always throws InvalidCastException.

[Visual Basic] Overloads Public Shared Function ToDateTime(Char) As DateTime
[C#] public static DateTime ToDateTime(char);
[C++] public: static DateTime ToDateTime(__wchar_t);
[JScript] public static function ToDateTime(Char) : DateTime;

Returns the specified DateTime; no actual conversion is performed.

[Visual Basic] Overloads Public Shared Function ToDateTime(DateTime) As DateTime
[C#] public static DateTime ToDateTime(DateTime);
[C++] public: static DateTime ToDateTime(DateTime);
[JScript] public static function ToDateTime(DateTime) : DateTime;

Calling this method always throws InvalidCastException.

[Visual Basic] Overloads Public Shared Function ToDateTime(Decimal) As DateTime
[C#] public static DateTime ToDateTime(decimal);
[C++] public: static DateTime ToDateTime(Decimal);
[JScript] public static function ToDateTime(Decimal) : DateTime;

Calling this method always throws InvalidCastException.

[Visual Basic] Overloads Public Shared Function ToDateTime(Double) As DateTime
[C#] public static DateTime ToDateTime(double);
[C++] public: static DateTime ToDateTime(double);
[JScript] public static function ToDateTime(double) : DateTime;

Calling this method always throws InvalidCastException.

[Visual Basic] Overloads Public Shared Function ToDateTime(Short) As DateTime
[C#] public static DateTime ToDateTime(short);
[C++] public: static DateTime ToDateTime(short);
[JScript] public static function ToDateTime(Int16) : DateTime;

Calling this method always throws InvalidCastException.

[Visual Basic] Overloads Public Shared Function ToDateTime(Integer) As DateTime
[C#] public static DateTime ToDateTime(int);
[C++] public: static DateTime ToDateTime(int);
[JScript] public static function ToDateTime(int) : DateTime;

Calling this method always throws InvalidCastException.

[Visual Basic] Overloads Public Shared Function ToDateTime(Long) As DateTime
[C#] public static DateTime ToDateTime(long);
[C++] public: static DateTime ToDateTime(__int64);
[JScript] public static function ToDateTime(long) : DateTime;

Converts the value of the specified Object to a DateTime.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDateTime(Object) As DateTime
[C#] public static DateTime ToDateTime(object);
[C++] public: static DateTime ToDateTime(Object*);
[JScript] public static function ToDateTime(Object) : DateTime;

Calling this method always throws InvalidCastException. This method is not CLS-compliant.

[Visual Basic] Overloads Public Shared Function ToDateTime(SByte) As DateTime
[C#] public static DateTime ToDateTime(sbyte);
[C++] public: static DateTime ToDateTime(char);
[JScript] public static function ToDateTime(SByte) : DateTime;

Calling this method always throws InvalidCastException.

[Visual Basic] Overloads Public Shared Function ToDateTime(Single) As DateTime
[C#] public static DateTime ToDateTime(float);
[C++] public: static DateTime ToDateTime(float);
[JScript] public static function ToDateTime(float) : DateTime;

Converts the specified String representation of a date and time to an equivalent DateTime.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDateTime(String) As DateTime
[C#] public static DateTime ToDateTime(string);
[C++] public: static DateTime ToDateTime(String*);
[JScript] public static function ToDateTime(String) : DateTime;

Calling this method always throws InvalidCastException. This method is not CLS-compliant.

[Visual Basic] Overloads Public Shared Function ToDateTime(UInt16) As DateTime
[C#] public static DateTime ToDateTime(ushort);
[C++] public: static DateTime ToDateTime(unsigned short);
[JScript] public static function ToDateTime(UInt16) : DateTime;

Calling this method always throws InvalidCastException. This method is not CLS-compliant.

[Visual Basic] Overloads Public Shared Function ToDateTime(UInt32) As DateTime
[C#] public static DateTime ToDateTime(uint);
[C++] public: static DateTime ToDateTime(unsigned int);
[JScript] public static function ToDateTime(UInt32) : DateTime;

Calling this method always throws InvalidCastException. This method is not CLS-compliant.

[Visual Basic] Overloads Public Shared Function ToDateTime(UInt64) As DateTime
[C#] public static DateTime ToDateTime(ulong);
[C++] public: static DateTime ToDateTime(unsigned __int64);
[JScript] public static function ToDateTime(UInt64) : DateTime;

Converts the value of the specified Object to a DateTime using the specified culture-specific formatting information.

Supported by the .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;

Converts the specified String representation of a number to an equivalent DateTime using the specified culture-specific formatting information.

Supported by the .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;

Example

[Visual Basic, C#, C++] The following code example converts String representations of DateTime values with the ToDateTime method, using an IFormatProvider object.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of ToDateTime. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
' 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] 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.

See Also

Convert Class | Convert Members | System Namespace

Show:
© 2015 Microsoft