On my SQL server, I have a very simple testing table that contains only three rows: ID , Date and Hours . ( varchar , DateTime , varchar ).
SQL's DateTime format is similar: yyyy-MM-dd HH:mm:ss.fff .
The C # DateTime format is as follows: yyyy-dd-MM HH:mm:ss.fff .
I use the following code to get the C # DateTime format:
string ddd = "2012-10-10 00:00:00.000"; dt1 = DateTime.ParseExact(ddd, "yyyy-MM-dd HH:mm:ss.fff", null);
If I try to do this in yyyy-dd-MM , I get an error message:
DateTime, which is represented by a string, is not supported by calendaring.
In my C # application, I am trying to find the total hours between sunny dates. I kind of got it for work, but it only works for dates between 01 and 12.
So, from 2012-01-10 to 2012-10-10 (ten days) will give me the correct number of hours in the database.
But when I write 2012-01-10 to 2012-14-10 (fourteen days), I get an error message:
Convert char data type to out of range date and time data type
Thanks in advance.
PS. Can you suggest an easier way to get dates?
mySQL query
string CommandText = "SELECT * FROM date_test WHERE id = '4' AND date BETWEEN '" + dt1 + "' AND '" + dt2 + "'";
I figured out a problem, but not a solution.
The problem is that the SQL database is looking for the format and wants yyyy-MM-dd, but C # can only send yyyy-dd-MM.
Why can't ParseExact() do yyyy-MM-dd?