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,
Exceptions
| 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, and TryParseExact 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,
- if (!string.IsNullOrEmpty(dateString))
- {
- DateTime date = DateTime.Parse(dateString);
- }
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.
Note
Source Code is attached for better understanding.
Reference Links:

Fatma ZayedPosted Nov 9, 2022, 7:40 AM
Thanks it works perfectly
Cheruiyot KiruiPosted May 26, 2022, 10:10 AM
You can also inject this Depending on your Culture public void ConfigureServices(IServiceCollection services) { var cultureInfo = new CultureInfo("en-GB"); CultureInfo.DefaultThreadCurrentCulture = cultureInfo; CultureInfo.DefaultThreadCurrentUICulture = cultureInfo; }
Cheruiyot KiruiPosted May 10, 2022, 12:27 PM
Awesome, the last option helped.
Akhter HUssainPosted Nov 24, 2018, 10:53 PM
AOA, i have done same as you define above but still exception is raised String Was Not Recognized As A Valid DateTime
Iqrar AliPosted Dec 9, 2016, 9:46 AM
Very knowledgeable piece of data.
Thiruppathi RPosted Jun 12, 2016, 12:56 PM
Helpful article
Bhuvanesh MohankumarPosted May 13, 2016, 10:35 AM
Good one
Asfend YarPosted Feb 29, 2016, 1:41 PM
good
Raj ChandaPosted Feb 24, 2016, 2:20 AM
Try This,it works fine.. The date can also be converted using substring Dim dd As String = str.Substring(0, 2) Dim mm As String = str.Substring(3, 2) Dim yy As String = str.Substring(6, 4) fdate = yy & "-" & mm & "-" & dd
Ankur MistryPosted Dec 8, 2015, 7:23 AM
halpful article thanks for sharing
Humayun Kabir MamunPosted Dec 8, 2015, 2:50 AM
Nice..
Mohsin AzamPosted Dec 8, 2015, 1:42 AM
its nice,nice article and easy to understand (y)
Sridhar SharmaPosted Dec 8, 2015, 1:30 AM
Thanks for Sharing article :)