How to Convert String to DateTime in C#.NET

C# .NET provides several ways to convert a string into a DateTime object. This is typically required when reading data from a source that returns dates and times as strings. Here’s how you can do it. Let us check out, how to convert string to datetime in C#.Net.

Using DateTime.Parse() Method

This method attempts to convert a string to DateTime. If the conversion is unsuccessful, it will throw a FormatException.

Here’s an example of how you can use the DateTime.Parse() method:

string dateString = "05/24/2023";
DateTime dateTime = DateTime.Parse(dateString);
Console.WriteLine(dateTime);

In the above example, the string “05/24/2023” is converted to a DateTime object.

Using DateTime.ParseExact() Method

The DateTime.ParseExact() method converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly or an exception is thrown.

Here’s an example:

string dateString = "24-05-2023";
string format = "dd-MM-yyyy";
DateTime dateTime = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);
Console.WriteLine(dateTime);

In the above example, the string “24-05-2023” is converted to a DateTime object using the “dd-MM-yyyy” format.

Convert String to DateTime in Csharp

Using DateTime.TryParse() Method

The DateTime.TryParse() method is similar to the DateTime.Parse() method but it doesn’t throw an exception if the conversion fails. It returns a Boolean value that indicates whether the conversion succeeded or failed.

Here’s an example:

string dateString = "05/24/2023";
DateTime dateTime;
bool result = DateTime.TryParse(dateString, out dateTime);
if(result)
{
    Console.WriteLine(dateTime);
}
else
{
    Console.WriteLine("Invalid date string");
}

In the above example, if the string “05/24/2023” is successfully converted to a DateTime object, it is printed to the console. If the conversion fails, “Invalid date string” is printed.

Using DateTime.TryParseExact() Method

The DateTime.TryParseExact() method converts the specified string representation of a date and time to its DateTime equivalent using the specified format. It returns a Boolean value that indicates whether the conversion succeeded or failed.

Here’s an example:

string dateString = "24-05-2023";
string format = "dd-MM-yyyy";
DateTime dateTime;
bool result = DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime);
if(result)
{
    Console.WriteLine(dateTime);
}
else
{
    Console.WriteLine("Invalid date string");
}

In the above example, if the string “24-05-2023” is successfully converted to a DateTime object using the “dd-MM-yyyy” format, it is printed to the console. If the conversion fails, “Invalid date string” is printed.

Remember to include using System.Globalization; at the beginning of your script when using CultureInfo.

Convert string to datetime in C# mm/dd/yyyy

To convert a string to DateTime in C# with the format “MM/dd/yyyy”, you can use the DateTime.ParseExact() or DateTime.TryParseExact() method.

Here is an example of using DateTime.ParseExact():

using System;
using System.Globalization;

public class Program
{
    public static void Main()
    {
        string dateString = "05/24/2023";
        string format = "MM/dd/yyyy";
        DateTime dateTime = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);
        Console.WriteLine(dateTime);
    }
}

In this example, the string “05/24/2023” is converted to a DateTime object using the “MM/dd/yyyy” format.

Here is an example of using DateTime.TryParseExact():

using System;
using System.Globalization;

public class Program
{
    public static void Main()
    {
        string dateString = "05/24/2023";
        string format = "MM/dd/yyyy";
        DateTime dateTime;
        bool result = DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime);
        if(result)
        {
            Console.WriteLine(dateTime);
        }
        else
        {
            Console.WriteLine("Invalid date string");
        }
    }
}

In this example, if the string “05/24/2023” is successfully converted to a DateTime object using the “MM/dd/yyyy” format, it is printed to the console. If the conversion fails, “Invalid date string” is printed.

Convert string to date c# dd/mm/yyyy without time

To convert a string to a date without time in C# with the format “dd/MM/yyyy”, you can use the DateTime.ParseExact() or DateTime.TryParseExact() method.

Here is an example using DateTime.ParseExact():

using System;
using System.Globalization;

public class Program
{
    public static void Main()
    {
        string dateString = "24/05/2023";
        string format = "dd/MM/yyyy";
        DateTime dateTime = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);

        // Getting date part only
        DateTime datePart = dateTime.Date;

        Console.WriteLine(datePart.ToString("d")); // Output: 24/05/2023
    }
}

In this example, the string “24/05/2023” is converted to a DateTime object using the “dd/MM/yyyy” format, and the time component is removed by using the Date property of the DateTime object.

Here is an example using DateTime.TryParseExact():

using System;
using System.Globalization;

public class Program
{
    public static void Main()
    {
        string dateString = "24/05/2023";
        string format = "dd/MM/yyyy";
        DateTime dateTime;

        bool result = DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime);
        if(result)
        {
            // Getting date part only
            DateTime datePart = dateTime.Date;

            Console.WriteLine(datePart.ToString("d")); // Output: 24/05/2023
        }
        else
        {
            Console.WriteLine("Invalid date string");
        }
    }
}

In this example, if the string “24/05/2023” is successfully converted to a DateTime object using the “dd/MM/yyyy” format, the time component is removed and the date part is printed to the console. If the conversion fails, the “Invalid date string” is printed.

Considerations

When parsing dates, be aware of the following:

  • If the year is defined with two digits, the values between 00-29 are interpreted as the years 2000-2029, and the values between 30-99 are interpreted as the years 1930-1999.
  • If the date string doesn’t include a time component, the time value in the resulting DateTime object is midnight.
  • If the date string includes a time component, the time value is preserved in the resulting DateTime object.

I hope this tutorial helps you convert string to DateTime in C#.Net.

You may also like: