DateTime.TryParse
A string
may contain a valid time representation. But there is a possibility it is invalid. There is a way to convert safely the string
to a DateTime
.
We convert the string
into a DateTime
instance using the DateTime.TryParse
method. This makes errors easy to recover from. A boolean (ok) is returned.
Here we test the DateTime.TryParse
method. This is useful—it does the same thing as DateTime.Parse
, but does not throw any exceptions.
TryParse
returns true if the parse succeeded, and false otherwise. The bool
indicates whether the parse was "ok."TryParse
in the if conditional, and it fills the out DateTime
parameter.TryParse
in an if
-statement, but often this pattern is used to see if the parsing succeeded.TryParse
returns true and then false, indicating the first string
contained a valid date representation and the second did not.using System; // Use DateTime.TryParse when input is valid. string input = "2000-02-02"; DateTime dateTime; if (DateTime.TryParse(input, out dateTime)) { Console.WriteLine(dateTime); } // Use DateTime.TryParse when input is bad. string badInput = "???"; DateTime dateTime2; if (DateTime.TryParse(badInput, out dateTime2)) { Console.WriteLine(dateTime2); } else { Console.WriteLine("Invalid"); // <-- Control flow goes here }2/2/2000 12:00:00 AM Invalid
TryParseExact
Next we see the DateTime.TryParseExact
method. TryParseExact
enhances performance and makes your program simpler when you have to deal with lots of invalid date strings.
InvariantCulture
value is found in System.Globalization
, so that namespace must be specified.TryParseExact
method succeeds. True is returned by TryParseExact
.using System; using System.Globalization; string dateString = "Mon 16 Jun 8:30 AM 2008"; string format = "ddd dd MMM h:mm tt yyyy"; DateTime dateTime; if (DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime)) { Console.WriteLine(dateTime); }6/16/2008 8:30:00 AM
When you need DateTime.TryParseExact
, you are usually dealing with invalid formats of dates, or nonexistent dates.
DateTime.TryParseExact
will return false.DateTime.TryParseExact
method receives a formatting string
and converts an input string
into a DateTime
instance.string
must adhere to the standard .NET Framework style. This requirement steepens the learning curve.using System; using System.Globalization; string dateString = "???"; string format = "ddd dd MMM h:mm tt yyyy"; DateTime dateTime; if (DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime)) { Console.WriteLine(dateTime); } else { Console.WriteLine("Not a date"); }Not a date
If you are assured of the validity of your string
input (from previous testing in the code), you can use the DateTime.Parse
method instead.
Parse
has somewhat simpler syntax and is likely faster on valid input. On invalid input, Parse()
will not work as well.ParseExact
and TryParseExact
. They provide a way to assert more control over the parsing algorithm.We used the DateTime.TryParse
public static
method in the C# language. And we proved that it works on different types of strings without throwing exceptions.
The TryParse
method uses the tester-doer pattern. It is ideal when you are not positive your input will be valid and want to add error handling or recovery for invalid dates.