Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a string to datetime from "yyyy-MM-dd"

Even though it seems like this question has been asked a bunch of times, I can't seem to find an answer that is specific to my question:

I have a variable that is read from an XML file by a C# XML parser. It is a string, and is in the format "yyyy-MM-dd". I would like to read this variable into our database using SQL, but it needs to be in the proper datetime format in order for me to do this. Unfortunately, I can't find any datetime formats for "yyyy-MM-dd". Am I missing something?

I would prefer to be able to convert the variable to datetime before I have to open my SQL connection, if possible.

like image 946
weskpga Avatar asked Jul 18 '12 22:07

weskpga


People also ask

How do I convert a string to a date?

Using strptime() , date and time in string format can be converted to datetime type. The first parameter is the string and the second is the date time format specifier. One advantage of converting to date format is one can select the month or date or time individually.

How do I convert string to datetime in Python?

We can convert a string to datetime using strptime() function. This function is available in datetime and time modules to parse a string to datetime and time objects respectively.

How do I format a date in YYYY-MM-DD in Python?

strftime() Function to Format Date to YYYYMMDD in Python. The strftime() is used to convert a datetime object to a string with a given format. We can specify the format for YYYY-MM-DD in the function as %Y-%m-%d . Note that this function returns the date as a string.

What is the difference between Strftime and Strptime in Python?

strptime is short for "parse time" where strftime is for "formatting time". That is, strptime is the opposite of strftime though they use, conveniently, the same formatting specification.


1 Answers

I would prefer to be able to convert the variable to datetime before I have to open my SQL connection, if possible.

DateTime result = DateTime.ParseExact("2012-04-05", "yyyy-MM-dd", CultureInfo.InvariantCulture);
like image 51
Jorge Ferreira Avatar answered Sep 23 '22 06:09

Jorge Ferreira