Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert.DateTime throws error: String was not recognized as a valid DateTime for "06-13-2012"

Tags:

c#

datetime

I am inserting a date into my database, the value which comes from:

s.theDate = Convert.ToDateTime("06-13-2012");

and I get the error, "String was not recognized as a valid DateTime". How do I resolve this?

like image 426
petko_stankoski Avatar asked Jun 13 '12 06:06

petko_stankoski


People also ask

What does it meaning string was not recognized as a valid DateTime?

The reason is failed in other computers is likely to be they have different Cultures, one computer could be trying to parse in American format MM/dd/yyyy and the other one is in dd/MM/yyyy. You can use DateTime. ParseExact() and specify the format and culture you are trying to parse.

How do I fix string is not recognized as a valid DateTime in VB net?

[FormatException: String was not recognized as a valid DateTime.] then it is telling you that whatever is in the text box cannot be recognised as a date. You need to either change the textbox contents so it is a valid date, or provide a custom date format for the date parser that matches the text box contents, or both.

How check DateTime is valid or not in C#?

Use the DateTime. TryParseExact method in C# for Date Format validation. They method converts the specified string representation of a date and time to its DateTime equivalent. It checks whether the entered date format is correct or not.


1 Answers

Try this:

DateTime.ParseExact("06-13-2012", "MM-dd-yyyy", System.Globalization.CultureInfo.InvariantCulture)
like image 135
Eren Ersönmez Avatar answered Oct 21 '22 13:10

Eren Ersönmez