Convert DateTime to MySQL TimeStamp

我的梦境 提交于 2020-01-06 14:17:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!