Click here to Skip to main content
15,877,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to convert string to date but the error would occur how to solve this problem?

error : Conversion from string "" to type 'Date' is not valid.
The string is in dd/mm/yyyy format and and I have used cdate and convert.todatetime functions but in both case conversion was not success-full.
Posted
Updated 24-Oct-10 20:30pm
v2

Error clearly state that the string is not Null or Empty. Put a check of String.IsNullOrEmpty(mystring), and then do the conversion. It would work.

UDPATE:
based on the changed error, try:
DateTime.ParseExact("18-12-2007", "dd-MM-yyyy", CultureInfo.InvariantCulture)<br />

This is needed to handle the date format you are using which is not a standard one.
 
Share this answer
 
v2
Comments
Sandeep Mewara 25-Oct-10 2:40am    
Check at UI doesnt make sure always. It might be becuase of some scenario it got missed. Do a simple DEBUG and you can see if the value was right or not.
Error clearly states: Conversion from string "" .........
So it's an empty string that is bring tried to be converted.
Rounak Hasan 25-Oct-10 2:54am    
o sorry this error was coming previously when then was no null checking....now it is showing exception message "the string is not a valid date" like this...
Sandeep Mewara 25-Oct-10 2:58am    
Well, answer changes based on the error! Hope you understand now!

try:
DateTime.ParseExact("18-12-2007", "dd-MM-yyyy", CultureInfo.InvariantCulture);
This needs to be done as your date format is not a standard one.
try using DateTime.Parse(string)
hope this helps you..
 
Share this answer
 
Comments
Rounak Hasan 25-Oct-10 2:37am    
DateTime.Parse(string) this will return 0 if it fails to convert which will be not accepted anyway...

string datestring = "20000101";
string date1 = DateTime.ParseExact(datestring, yyyyMMdd, null);
// ... or ...
DateTime dateResult;
if (!DateTime.TryParseExact(datestring, yyyyMMdd,
null, DateTimeStyles.AssumeLocal,
out dateResult))
dateResult = DateTime.MinValue;



Check this article too

http://www.codeproject.com/KB/cs/String2DateTime.asp[^]
 
Share this answer
 
v3
Comments
Rounak Hasan 25-Oct-10 2:35am    
my date string is going '25/10/2010' like this...
demouser743 25-Oct-10 2:42am    
Output should be Ok what exact o/p you need i will give u the answer
demouser743 25-Oct-10 2:43am    
what exact o/p you need i will give u the answer,
Rounak Hasan 25-Oct-10 2:52am    
basically the are two string in dd/mm/yyyy format and i want to compare them..................I have used
If CDate(TextBox1To.Text.Trim) > CDate(TextBox2Fm.Text.Trim) Then

but it is showing exception..........The error was "the string is not a valid date" like this............
demouser743 25-Oct-10 2:57am    
You can convert text from text box into DateTime object and use DateTime class methods or just subtract two DateTime objects which will give you TimeSpan objects. Then you can compare dates as per your requirements.
check out the below codes

string tt = "21/10/2010";
          DateTime dt = new DateTime();
          dt = DateTime.Parse(tt);


OR

DateTime Dt1 = new DateTime(Convert.ToInt16 (tt.Substring(6, 4)),
                              Convert.ToInt16(tt.Substring(3, 2)),
                              Convert.ToInt16(tt.Substring(0, 2)));
 
Share this answer
 
VB
If (strDate<> String.Empty) Then
               lblDate.Text = Convert.ToDateTime(strDate).ToString("dd/mm/yyyy")
           End If
 
Share this 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