Resolve ‘String Was Not Recognized As A Valid DateTime’ Error

Today we will see the using of DateTime in code, possible mistakes while using and handling techniques to resolve the errors coming by using the method.

While analyzing another issue in one of my projects I came across this error and after a time when i was successful in handling it, so I thought to share the resolution to the best of my understanding.

According to MSDN Knowledge base definition the Datetime represents an instant in time, typically expressed as a date and time of day.

We use different Datetime methods to convert it to proper datetime to be accessible for compiler to process it from strings.

In project we are using Datepicker plugins or take value as string from those datapickers and give to the system for further processing and we use different DateTime methods to convert it to proper date and time format but the problem arises when we are using the different patterns of datetime depending on either for specific culture or on different string format.

There are different methods to do the job but i will discuss 3 of them those are common and useful. Let us start with the first one which is DateTime.Parse, it has 3 overloads and the most possible exception comes due to the following,


Exception Condition
ArgumentNullException s is null.
FormatException s does not contain a valid string representation of a date and time.

Now we will discuss the first and the simplest overload which takes string as param like this.

By parsing the string representation of a date and time value. The Parse, ParseExact, TryParse, andTryParseExact methods all convert a string to its equivalent date and time value. The following example uses the Parse method to parse a string and convert it to a DateTimevalue.

string dateString = “20/5/2015 3:47:52 PM”;

DateTime date = DateTime.Parse(dateString);

The above below code performs well and what about if you have empty string,

string dateString = “”;

DateTime date = DateTime.Parse(dateString);

It generates the Exception of type System.FormatException as elaborated above as there are two possible exceptions related to it.

The best way to handle at the initial point is to check the string for either null or empty string like this,

  1. if (!string.IsNullOrEmpty(dateString))
  2. {
  3.     DateTime date = DateTime.Parse(dateString);
  4. }

And if your value is not null but also format is not defined then an exception is thrown. To avoid the scenario use the following code to handle it by giving appropriate culture to avoid the error.

string dateString = “20/5/2015”;
DateTime date = Convert.ToDateTime(dateString, System.Globalization.CultureInfo.GetCultureInfo(“ur-PK”).DateTimeFormat);

and for other methods (ParseExact, TryParse, and TryParseExact) same approach is used to bypass the exception.



Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s