It converts specified string data to equivalent datetime and returns Boolean value after parsing which indicates parsing is succeeded. It is available in System (mscorlib.dll) namespace and introduced .NET framework 2.0 onwards. It contains the following overload methods:
TryParse() always try to parse the string value datetime. If conversion succeeded then it returns correct DateTime value and MinValue(1/1/0001 12:00:00 AM) if conversion failed. If string value is null or empty and you are trying to convert it DateTime then it returns MinValue only. Secondly, it always returns a Boolean value which indicates conversion succeeded or failed. If conversion succeeded then True otherwise it returns False.
It is most likely as DateTime.Parse(). But only difference is that it doesn’t throw any exception when conversion failed rather it returns MinValue of DateTime.
It converts specified string to equivalent DateTime with specified format and culture. The string value of format must match with string value of datetime. It is available in System (mscorlib.dll) namespace and introduced .NET framework 2.0 onwards. It contains tje following overload methods:
DateTime.ParseExact(string value, string format, IFormatProvider provider, DateTimeStyles style)
Value: It is a string representation of date and time.
Format: It is format specifier that defines a date look like after conversion.
Provider: It is an object which specify about culture info.
Style: It defines the formatting options that customize string parsing for some date and time parsing methods.
DateTime.ParseExact(string value, string[] formats, IFormatProvider provider, DateTimeStyles style)
Value: It is a string representation of date and time.
Formats: It is format specifier that defines a date look like after conversion. It is a string array which contains a list of formats and at least one format must match with string (value) to convert DateTime object.
Provider: It is an object which specify about culture info.
Style: It defines the formatting options that customize string parsing for some date and time parsing methods.
It returns MinValue( 1/1/0001 12:00:00 AM) if the following condition satisfies:
- string value is null.
- string value is blank.
- string is not correct date.
- string is not match with format provided.
It throws exception only if DateTimeStyle value is not valid otherwise returns MinValue.
Secondly, it always returns a Boolean value (true or false) to indicate whether conversion succeeded. It returns True if conversion succeeded and False if conversion failed.
The following are some examples that will help you to understand it better:
- string dateString = null;
- CultureInfo provider = CultureInfo.InvariantCulture;
- DateTime dateTime10;
- bool isSuccess1 = DateTime.TryParseExact(dateString, "MM/dd/yyyy", provider, DateTimeStyles.None, out dateTime10);
- dateString = "not a date";
-
- DateTime dateTime11;
- bool isSuccess2 = DateTime.TryParseExact(dateString, "MM/dd/yyyy", provider, DateTimeStyles.None, out dateTime11);
- dateString = "Tue Dec 30, 2015";
- DateTime dateTime12;
-
- bool isSuccess3 = DateTime.TryParseExact(dateString, "MM/dd/yyyy", provider, DateTimeStyles.None, out dateTime12);
- dateString = "10-22-2015";
- DateTime dateTime13;
- bool isSuccess4 = DateTime.TryParseExact(dateString, "MM/dd/yyyy", provider, DateTimeStyles.None, out dateTime13);
- dateString = "10-22-2015";
- DateTime dateTime15;
- bool isSuccess5 = DateTime.TryParseExact(dateString, "MM-dd-yyyy", provider, DateTimeStyles.None, out dateTime15);
- dateString = "10-12-2015";
-
- DateTime dateTime14;
- bool isSuccess6 = DateTime.TryParseExact(dateString, new string[]{ "MM/dd/yyyy", "MM-dd-yyyy", "MM.dd.yyyy"}, provider, DateTimeStyles.None, out dateTime14);
Analysis
The DateTime struct has couple of methods for parsing a string into a DateTime. We will discuss about individual differences:
Difference between Parse() and ConvertToDateTime()
Both these two methods are almost similar except the following differences:
If string value is null then Parse() throws Exception while ConvertToDateTime() returns DateTime.MinValue.
In Parse you can pass one extra parameter called DataTimeSyles which is not available ConvertToDateTime().
Lastly, Convert.ToDateTime uses DateTime.Parse internally, with the current culture.
Difference between Parse() and ParseExact()
Parse() and ParseExact() are quite similar. However, in ParseExact() we can pass format as extra parameter which is not available in Parse(). Format parameter helps to convert a string date value to DateTime object when a date is different format like “11-23-2015”(Format should be “MM-dd-yyyy”).
Difference between Parse() and TryParse()
The DateTime.TryParse(), method is similar to the DateTime.Parse(String) method, except that the DateTime.TryParse() method does not throw an exception if the conversion fails. DateTime.TryParse() always returns DateTime.MinValue if conversion fails but Parse() throws exception.
Difference between DateTime.TryParse() and DateTime.TryParseExact()
DateTime.TryParse() and DateTime.TryParseExact() are similar except format parameter. DateTime.TryParseExact() uses a extra parameter for format which is not available in DateTime.TryParse(). Format parameter helps to convert some custom string format. But DateTime.TryParse() returns DateTimenMinValue if any custom date is provided.
Thus, use TryParse() when you want to to attempt a parse and handle invalid data immediately (instead of throwing the exception), and ParseExact() when the format you are expecting is not a standard format, or when you want to limit to one particular standard format for efficiency.
If you're sure the string is a valid DateTime, and you know the format, you could also consider the DateTime.ParseExact() or DateTime.TryParseExact() methods.
Conclusion
In the article we discussed about parsing DateTime in C# in different ways and what are the differences between them. Choose appropriate method as per data. Hope this helps.