-2

I have string "24-04-2015 00:00:00". I need convert it into datetime and store it in database.

If i pass "24-04-2015 00:00:00" as a string to database, I am getting the error

"Failed to convert parameter value from a String to a DateTime."

And also if i use DateTime.ParseExact("24-04-2015 00:00:00", "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture); I am getting below error

String was not recognized as a valid DateTime.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • 2
    Possible duplicate of [Converting a String to DateTime](https://stackoverflow.com/questions/919244/converting-a-string-to-datetime) – Esko Aug 22 '17 at 11:06
  • 1
    Clearly `24-04-2015` and `yyyy-MM-dd` don't match. Did you do any research on this subject? – mason Aug 22 '17 at 12:19
  • "yyyy-MM-dd HH:mm:ss" won't match your input string. Swap yyyy and dd, and it should work. Also, beware of Time Zone : is your time is local hour or UTC hour ? You can specify this with the last parameter (enum DateTimeStyles or something like this) of ParseExact method. – AFract Aug 22 '17 at 12:21

1 Answers1

1

Try below:

//Add namespace 
using System.Globalization;

// Code to convert the string to datetime
string datetime="24-04-2015 00:00:00";
DateTime dt = DateTime.ParseExact(datetime, "dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture);

Above code is tested.

Karthick Raju
  • 757
  • 8
  • 29