Problem when converting string to a date

someone could help me,

When I receive a text through the selection option of a web page, I want to convert it to a date and time, but I get this error in the ExceptionDetail.
probably created by IncludeExceptionDetailInFaults = true,
whose value is:
System.FormatException: the string was not recognized as a valid DateTime.

Hi @jalberto,

Can you share an example of your string. Also you can try this out:


“INSERT INTO documentodetalle (status, fechaInicio, fechaCierre, localidad, Entidad, codigoPostal, Clientes_idClientes) VALUES (‘’, '”+convert.ToDateTime(fechaInicioC.ToString).ToString(“yyyy/MM/dd HH: mm: ss tt”)+“‘,’”+fechaTerminoC+“', '”+Entidad+“', '”+localidad+“', '”+Cp+“‘,’”+row(0).ToString+“')”

What value is being passed in fechaInicioC?

is String obtained of get text

DateTime.ParseExact(fechaInicioC.toString, “yyyy-MM-dd hh:mm:ss tt”,

Hei @jalberto
That validation error means you have a datetime type and so you need to convert it to a string.

DateTime.ParseExact( alkfslkjfldksjlkfj ) returns a date, so at the end of that part just add a .ToString
DateTime.ParseExact( alkfslkjfldksjlkfj ).ToString(format)
where format is the format string you want to use (if necessary)
DateTime.ParseExact( alkfslkjfldksjlkfj ).ToString(“yyyy/MM/dd hh:mm:ss tt”)

Another thing is that the format string inside the ParseExact() needs to be the format that the date is in. Looking at your image it shows the date like “dd/MM/yyyy hh:mm tt”

So, your ParseExact should be like this:
DateTime.ParseExact(fechalnicioC.ToString, "dd/MM/yyyy hh:mm tt", System.Globalization.CultureInfo.InvariantCulture).ToString("yyyy/MM/dd hh:mm:ss tt")

And like I said, the format in the ParseExact() is the format the initial string is in, and the format in the .ToString() is the format you want to change the date to.

I hope that helps get it working.



I will try it that way

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.