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.
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.
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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With