Convert string to datetime from "yyyy-MM-dd"

Even if it seems that this question has been asked several times, I can’t find an answer that is relevant to my question:

I have a variable that is read from an XML file using a C # XML parser. This is a string and is in format "yyyy-MM-dd". I would like to read this variable in our database using SQL, but it must be in the correct date and time format so that I can do this. Unfortunately, I can not find datetime formats for "yyyy-MM-dd". Did I miss something?

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

+5
source share
5 answers

I would rather 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);
+16
source

Perhaps the ParseExact function might help you.

DateTime dResult = DateTime.ParseExact("2012-07-18", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
+1
source

LINQ to XML, DateTime:

DateTime date = (DateTime) element;

SQL. : , , , vanilla.NET DateTime. , IMO.

LINQ to XML, smink Riera - LINQ to XML, :

+1

in sql server you can use
SELECT CONVERT query (VARCHAR (10), GETDATE (), 120) AS [YYYY-MM-DD]

+1
source

If you are not sure if this will work, you can use "TryParseExact". If it works "success", it will be true. Thus, you do not risk the exception of parsing.

DateTime timestamp;
bool success = DateTime.TryParseExact("2015-12-25", "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out timestamp);
0
source

All Articles