简体   繁体   English

将DateTime转换为MySQL TimeStamp

[英]Convert DateTime to MySQL TimeStamp

I am doing this to save a DateTime in the MySQL database but when stored, the value is 2011-10-30 06:01:07 . 我这样做是为了将DateTime保存在MySQL数据库中,但存储时的值为2011-10-30 06:01:07 06 is supposed to be pm , not am : 06应该是pm ,而am

startTime.ToString("yyyy-MM-dd hh:mm:ss");

Simply do this 只需这样做

startTime.ToString ("yyyy-MM-dd HH:mm:ss");

where HH (capital H) shows 24 hours format. 其中HH (大写H)显示24小时格式。 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 因此,当您尝试保存2011-10-30 06:01:07它应该是am并且当您想要pm您应该保存2011-10-30 18:01:07

Add the value using parameters : 使用parameters添加值:

MySqlCOmmand cmd=new MySqlCommand(
    "INSERT INTO MyTable(myDate) VALUES(?myDate)", connection);
cmd.Parameters.AddWithValue("?myDate", startTime);
cmd.ExecuteNonQuery();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM