I am working with DateTime objects to create a monitoring application.
The principle is that each time a user launches a specific application, the current date and time will be saved in the DB. Then in the monitoring app, I get this date field from DB and display it.
As I went through testing today, I tried to launch the app : DateTime.Now is stored (ie, in French culture, 22/04/11 17:09:50 ).
When I launch the monitoring app, what I get from the database is 22/04/11 00:00:00
Is there any reason why the exact time isn’t saved?
The column in DB is, of course, a Date column.
Some code:
Storing the date:
command = new MySqlCommand(_updateDateCommand, connection);
command.Parameters.AddWithValue("?Template", app);
command.Parameters.AddWithValue("?Date", DateTime.Now);
command.Parameters.AddWithValue("?Id", u.Id);
if (command.ExecuteNonQuery() != 0) return true;
Retrieving the data:
object date = reader.GetValue(4);
if (date == System.DBNull.Value)
{
tempUserApp.DateLastUsed = DateTime.MinValue;
}
else
{
tempUserApp.DateLastUsed = (DateTime)date;
}
I’m sure that the problem comes from the storing process, because visualizing data with a tool such as Toad show me 22/04/11 00:00:00 in the Date field.
Update command:
private readonly string _updateDateCommand =
"UPDATE userapplications " +
"JOIN template t ON t.Name = ?Template " +
"SET DateUsed=?Date WHERE `User Id`=?Id AND `Template Id`=t.Id";
Is there any special formatting for MySQL dates?
I bumped into this StackOverflow question and tried the solution proposed, it does not work either
Any idea?
The DB Column should be DateTime, not Date. Look at the Docs.
About DATE:
About DATETIME:
Getting the Date return like dd/mm/yyyy 00:00:00 is misleading, it’s just being formatted by the tool as the default date/datetime format. What you really need is to set the DB column as DateTime.