Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys,
When i convert the label box data to date time . i got this error.
"String was not recognized as a valid DateTime."

i searched lot of solution ,did not work well.

1.MyDateTime = Convert.ToDateTime(MyString.Trim())

2. using Globalization.DateTimeFormatInfo.

above have not worked well. again get a same error...

regards ,
Saran.t
Posted
Comments
JF2015 30-Dec-10 7:38am    
Can you tell us the content of "MyString"?

If your string is in a different locale format, then you will get this. If your string is always in the same format (which may not necessarily be your locale) you could try using DateTime.TryParseExact http://msdn.microsoft.com/en-us/library/ms131044.aspx[^] with the appropriate string. The string format is listed here: Formatting a DateTime for display - format string description[^] - it is written for output but the format string info is the same for parsing.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 30-Dec-10 8:12am    
I'll be damned! You beat me to it! 5+
I second using DateTime.TryParseExact() and contribute this link to "Custom Date and Time Format Strings" on the page is an example of how to use DateTime.TryParseExact() and a list of the available format specs:
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
The contents of MyString is either not a valid DateTime representation, or the format of the otherwise valid DateTime string is not what the DateTime object is expecting.

A little googling goes a long way:

http://msdn.microsoft.com/en-us/library/1k1skd40.aspx[^]

Google for "vb.net datetime from string", and see what you come up with. Of course, you could just be a programmer and figure out what's going on in the debugger, but you should already know how to do that.

 
Share this answer
 
v2
It means the date time is not in a current format.

Since your profile says you are from India, I will assume you are using a format - dd/MM/yyyy.
This format is not correct for the default culture en-US (English-US).

So you basically have two options:
i) Use a correct format recognized by the default culture such as MM/dd/yyyy. OR,
ii) Change the culture to hi-IN (Hindi-India). This can be done for a specific string, or for the application. See here[^].
You may also like to read: Custom Date and Time Format Strings[^]
Search Google if you need more details on the above topics.

If my assumption is not correct, you may like to update your question for a better answer.
 
Share this answer
 
v3
you can try using NOW to know exactly wath format is using your machine and re-write your code fixing it.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 4-Jan-11 11:31am    
Reason vor my vote of 1:
The question was already answered and accepted. No added value present in your answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900