问题
I am doing this to save a DateTime in the MySQL database but when stored, the value is 2011-10-30 06:01:07. 06 is supposed to be pm, not am:
startTime.ToString("yyyy-MM-dd hh:mm:ss");
回答1:
Simply do this
startTime.ToString ("yyyy-MM-dd HH:mm:ss");
where HH (capital H) shows 24 hours format. So when you try saving 2011-10-30 06:01:07 it suppose to be am and when you want pm you should save 2011-10-30 18:01:07
回答2:
Add the value using parameters:
MySqlCOmmand cmd=new MySqlCommand(
"INSERT INTO MyTable(myDate) VALUES(?myDate)", connection);
cmd.Parameters.AddWithValue("?myDate", startTime);
cmd.ExecuteNonQuery();
来源:https://stackoverflow.com/questions/7944317/convert-datetime-to-mysql-timestamp