Table of Contents
This article will discuss the built-in functions that you can use to convert a string representation to its equivalent DateTime value using C#. The .NET framework provides several functions that developers should be aware of to convert a string to a DateTime type.
More like this:
C# string to DateTime Conversion
Below are the functions that you can, as a developer, use to convert a string to a DateTime.
Convert.ToDateTime()
This method has too many overloads; however, we will list the most used ones.
- ToDateTime(String): Converts a string to a DateTime datatype.
DateTime dateTime = Convert.ToDateTime("04/01/2021"); Console.WriteLine(dateTime);
- ToDateTime(String, IFormatProvider): Converts a string to a DateTime format, using specific formatting information.
using System.Globalization; DateTime dateTime = Convert.ToDateTime("04/01/2021", culture); Console.WriteLine(dateTime);[adinserter block=”2″]
DateTime Exceptions
The program will throw out an error if the string is not in the correct format.
DateTime dateTime = Convert.ToDateTime("04/021/2021"); Console.WriteLine(dateTime);
Will throw the error message below:
System.FormatException: 'String was not recognized as a valid DateTime.'
DateTime.Parse()
The DateTime.Parse
has few overloads. We will discuss the most used ones in detail below.
- DateTime.Parse(String): Converts a string to a Date
DateTime dateTime; string dateString = "2/16/2021 12:15:49 PM"; dateTime = DateTime.Parse(dateString); Console.WriteLine(dateTime);
- DateTime.Parse(String, IFormatProvider): Converts a string to a Date by using culture-specific format information.
DateTime dateTime = DateTime.ParseExact("01/01/2021", "yyyyMMddHHmmssfff", CultureInfo.InvariantCulture); Console.WriteLine(dateTime.ToString());
- Parse(String, IFormatProvider, DateTimeStyles): Converts a string to a DateTime using culture-specific format information and a formatting style.
DateTime dt = DateTime.Parse("2021/01/01", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal); Console.WriteLine(dt);
DateTime.ParseExact()
ParseExact is another method to convert a string representation of a DateTime to DateTime datatype. Below are some of the most used overloads of this function.
- ParseExact(String, String, IFormatProvider): Converts a string to a DateTime datatype using the specified format and culture-specific format.
string format = "d"; CultureInfo provider = CultureInfo.CurrentCulture; DateTime result = DateTime.ParseExact("01/31/2021", format, provider); Console.WriteLine(result.ToString());
- ParseExact(String, String, IFormatProvider, DateTimeStyles): Converts a string to a DateTime datatype using the specified format, culture-specific format information, and DateTime style.
CultureInfo enUS = new CultureInfo("en-US"); DateTime dateValue = DateTime.ParseExact("01/31/2021 10:30 AM", "g", enUS, DateTimeStyles.AdjustToUniversal); Console.WriteLine(dateValue);
DateTime.TryParse()
Another method provided by the .NET framework allows you to convert a string to a DateTime format. Below are the most used overloads for this method:
- TryParse(String, DateTime): Converts a string to DateTime datatype, then returns a value indicating if the conversion succeeded or not.
DateTime dateValue; var format = new[] { "dd/MM/yyyy", "yyyy-MM-dd" }; if (DateTime.TryParseExact("01/31/2021", format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateValue)) { Console.WriteLine("Date is valid!"); } else { Console.WriteLine("Date is invalid!"); }
Difference between DateTime.Parse(), DateTime.ParseExact() and DateTime.TryParse()
[adinserter block=”2″]
DateTime.Parse()
will throw an exception if it cannot parse the string value to its equivalence in DateTime, whereas DateTime.TryParse()
will return a boolean value based on the conversion result.
On the other hand, DateTime.ParseExact()
allows you to specify the string format that you are trying to parse.
Blog Hero