This documentation is archived and is not being maintained.

Convert.ToDateTime Method (String, IFormatProvider)

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

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public static DateTime ToDateTime (
	string value,
	IFormatProvider provider
)
public static DateTime ToDateTime (
	String value, 
	IFormatProvider provider
)
public static function ToDateTime (
	value : String, 
	provider : IFormatProvider
) : DateTime
Not applicable.

Parameters

value

A String containing a date and time to convert.

provider

An IFormatProvider interface implementation that supplies culture-specific formatting information.

Return Value

A DateTime equivalent to the value of value. -or- A DateTime equivalent to DateTime.MinValue if value is a null reference (Nothing in Visual Basic).

Exception typeCondition

FormatException

value is not a properly formatted date and time string.

The return value is the result of invoking the DateTime.Parse method on value.

provider is an IFormatProvider instance that obtains a DateTimeFormatInfo object. The DateTimeFormatInfo object provides culture-specific information about the format of value. If provider is a null reference (Nothing in Visual Basic), the DateTimeFormatInfo for the current culture is used.

The following code example converts String representations of date values with the ToDateTime method, using an IFormatProvider object.

// 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
*/

// Example of Convert.ToDateTime( String, IFormatProvider ).
import System.* ;
import System.Globalization.* ;

class StringToDateTimeDemo
{
    private static String lineFmt = "{0,-18}{1,-12}{2}";
      
    // Get the exception type name; remove the namespace prefix.
    public static String GetExceptionType(System.Exception ex) 
    {
        String exceptionType = ex.GetType().ToString();
        return exceptionType.Substring((exceptionType.LastIndexOf('.') + 1)) ;
    } //GetExceptionType   
   
    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();
        String dateStr = new String();
        for (int iCtr = 0; iCtr < dateStrings.length; iCtr++) {
            dateStr = dateStrings[iCtr];
      
            // Convert each string in the dateStrings array.
      
            DateTime dateTimeValue = new DateTime();
         
            // 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 (System.Exception ex) {
            
                // Display the exception type if Parse failed.
                Console.WriteLine("{0}", GetExceptionType(ex));
            }
        }
    } //StringToDateTime
    
    public static void main(String[] args)
    {
        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");
    } //main
} //StringToDateTimeDemo

/*
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
*/

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Show: