Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot implicitly convert type 'string' to 'System.DateTime'

Tags:

c#

datetime

I am trying to convert from string to DataTime but an an error occurs. I am using VS 2003, .NET Framework 1.1

DateTime dt = Convert.ToDateTime("11/23/2010");
string s2 = dt.ToString("dd-MM-yyyy");
DateTime dtnew = Convert.ToString(s2);

Cannot implicitly convert type 'string' to 'System.DateTime'

Can any one help me me with the syntax how to solve the error.

like image 984
happysmile Avatar asked Dec 21 '10 10:12

happysmile


2 Answers

string input = "21-12-2010"; // dd-MM-yyyy    
DateTime d;
if (DateTime.TryParseExact(input, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out d))
{
    // use d
}
like image 149
abatishchev Avatar answered Oct 12 '22 22:10

abatishchev


I guess that you have made a typo - change Convert.ToString(s2) to Convert.ToDateTime(s2).

like image 25
VinayC Avatar answered Oct 12 '22 22:10

VinayC