Date conversion error

I am developing a Windows application.

That I have a date in a string format like → fileDate="15/03/2013"

I want it to be converted to a date format, since the field is my database datetime.

I used the following things for this →

DateTime dt = DateTime.ParseExact(fileDate, "yyyyy-DD-MM", CultureInfo.InvariantCulture);

DateTime dt = DateTime.Parse(fileDate);

Both of these methods were unsuccessful, giving me an error →

String was not recognized as a valid DateTime.

What could be a mistake?

Is there any other way to do this?

+5
source share
6 answers

You must specify the date format according to the date string you must ParseExact . You Can See More on Custom DateTime Format - MSDN

Edit

"yyyy-MM-dd HH:ss"

For

"dd/MM/yyyy"

Your code will be

DateTime dt = DateTime.ParseExact(fileDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
+4
source
 string fileDate = "15/03/2013";
 DateTime dt = DateTime.ParseExact(fileDate, "dd/mm/yyyy", CultureInfo.InvariantCulture);
+6
source

:

DateTime dt = DateTime.ParseExact(fileDate, "dd/MM/yyyy",CultureInfo.InvariantCulture);

("dd/MM/yyyy") , fileDate.

+2

u SimpleDateFormat dateFormat = SimpleDateFormat ( "MM/dd/yyyy" ); convertDate = dateFormat.parse( "ur_dateString" )

+2

"yyyyy-DD-MM", , d d., 5 y s, 4, yyyy, : "dd/MM/yyyy". , "d/M/yyyy", /.

, :

string fileDate="15/03/2013";
DateTime dt = DateTime.ParseExact(fileDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);

DateTime - MSDN

+1

This is because the string "03/15/2013" cannot be parsed as a DateTime with the string format "yyyy-MM-dd HH: ss".

0
source

All Articles