String was not recognized as a valid DateTime.
C#, VBA, Java, SQL Server.
I am trying to convert my string formatted value to date type with format dd/MM/yyyy. I am getting the above exception in my application /website. I searched for the above error and found the following Solution.
Problem (Error) :
The error shown in question have been thrown by Convert.ToDateTime method.
Example: string dateString = @"30/06/1991";
DateTime date = Convert.ToDateTime(dateString);
In the above code, the dateString represents Date in the Day/Month/Year format.
By default, the en-US culture is used by .NET according to which the Date is in Month/Day/Year format. So, when ToDateTime method is used it throws error as 30 is out of the Month range.
Solution:
Method No. 1
To avoid this error the appropriate culture can be used as follows;
string dateString = @"30/06/1991";
DateTime date2 = Convert.ToDateTime(dateString,
System.Globalization.CultureInfo.GetCultureInfo("hi-IN").DateTimeFormat);
this Method we have explained in the tutorial video.
Method No. 2
the ParseExact method can be used with the required custom format as shown below:
string dateString = @"30/06/1991";
DateTime DOB= DateTime.ParseExact(dateString, @"d/M/yyyy",
System.Globalization.CultureInfo.InvariantCulture);
#dateTimeCoversionException
#dateTimeConvertError
Hafeez Computers Thakot
HCT
SQL date conversion | Convert Date format into DD/MM/YYYY format in SQL Server • sql date conversion | Convert Date format ...
RDLC Tablix sum error | visual Studio | Report error C#
• report viewer rdlc sum error | visual St...
how to add Microsoft RDLC Report Designer in Visual Studio C#? ( RDLC file open in XML code Solved)
• how to add Microsoft RDLC Report Designer ...
could not load file or assembly | Microsoft Report Viewer SQL Server | Visual Studio Application • could not load file or assembly | Microsof...
An error occurred When attaching the database | MDF file Cannot attach SQL Server Solved • An error occurred When attaching the datab...