Convert.ToDateTime Method in C#

The Convert.ToDateTime method in C# converts a string representation of a date and time to a DateTime object. This method is commonly used when you need to parse date strings from user input, files, or external data sources into a usable DateTime format.

The method supports various date formats and provides automatic parsing based on the current culture settings of your system.

Syntax

Following are the most commonly used overloads of Convert.ToDateTime

public static DateTime ToDateTime(string value)
public static DateTime ToDateTime(string value, IFormatProvider provider)
public static DateTime ToDateTime(object value)

Parameters

  • value − A string or object that represents a date and time to convert.
  • provider − An object that supplies culture-specific formatting information (optional).

Return Value

Returns a DateTime object that represents the date and time equivalent of the input value.

Using Convert.ToDateTime with String Input

Example

using System;

public class Demo {
    public static void Main() {
        string myDateString;
        DateTime result;
        myDateString = "09/09/2018";
        result = Convert.ToDateTime(myDateString);
        Console.WriteLine("'{0}' converted to {1}", myDateString, result);
        
        // Additional examples with different formats
        string timeString = "2:30 PM";
        DateTime timeResult = Convert.ToDateTime(timeString);
        Console.WriteLine("'{0}' converted to {1}", timeString, timeResult);
        
        string fullDateTime = "December 25, 2023 3:45:30 PM";
        DateTime fullResult = Convert.ToDateTime(fullDateTime);
        Console.WriteLine("'{0}' converted to {1}", fullDateTime, fullResult);
    }
}

The output of the above code is −

'09/09/2018' converted to 9/9/2018 12:00:00 AM
'2:30 PM' converted to 1/1/0001 2:30:00 PM
'December 25, 2023 3:45:30 PM' converted to 12/25/2023 3:45:30 PM

Using Convert.ToDateTime with Culture-Specific Formatting

Example

using System;
using System.Globalization;

public class CultureDemo {
    public static void Main() {
        string europeanDate = "25/12/2023";
        
        // Using US culture (MM/dd/yyyy)
        CultureInfo usCulture = new CultureInfo("en-US");
        
        // Using European culture (dd/MM/yyyy)
        CultureInfo europeanCulture = new CultureInfo("en-GB");
        
        try {
            DateTime euroResult = Convert.ToDateTime(europeanDate, europeanCulture);
            Console.WriteLine("European format: '{0}' converted to {1}", europeanDate, euroResult);
        }
        catch (FormatException e) {
            Console.WriteLine("Error: " + e.Message);
        }
        
        // Convert null value
        DateTime nullResult = Convert.ToDateTime(null);
        Console.WriteLine("Null converted to: {0}", nullResult);
    }
}

The output of the above code is −

European format: '25/12/2023' converted to 12/25/2023 12:00:00 AM
Null converted to 1/1/0001 12:00:00 AM

Handling Invalid Date Strings

Example

using System;

public class ErrorHandlingDemo {
    public static void Main() {
        string[] dateStrings = { 
            "valid date: 01/15/2023",
            "invalid date: 13/25/2023",
            "empty string: ",
            "text: Hello World"
        };
        
        foreach (string dateStr in dateStrings) {
            string[] parts = dateStr.Split(':');
            string testValue = parts.Length > 1 ? parts[1].Trim() : parts[0];
            
            try {
                if (!string.IsNullOrEmpty(testValue)) {
                    DateTime result = Convert.ToDateTime(testValue);
                    Console.WriteLine("Success: '{0}' -> {1}", testValue, result);
                } else {
                    Console.WriteLine("Skipping empty string");
                }
            }
            catch (FormatException) {
                Console.WriteLine("Error: '{0}' is not a valid date format", testValue);
            }
        }
    }
}

The output of the above code is −

Success: '01/15/2023' -> 1/15/2023 12:00:00 AM
Error: '13/25/2023' is not a valid date format
Skipping empty string
Error: 'Hello World' is not a valid date format

Common Use Cases

  • User Input Processing − Converting date strings entered by users in forms or console applications.
  • File Data Parsing − Reading date information from CSV files, configuration files, or log files.
  • Database Operations − Converting date strings retrieved from databases or APIs.
  • Data Migration − Converting date formats when transferring data between different systems.

Conclusion

The Convert.ToDateTime method provides a flexible way to convert string representations into DateTime objects. It automatically handles various date formats and supports culture-specific parsing, making it essential for processing date data from different sources in C# applications.

Updated on: 2026-03-17T07:04:35+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements