我在将基于字符串的日期时间格式转换为 mysql 日期时间格式时遇到困难。
我尝试了以下方法
str latesttime = "2\/11\/2015 8:04:06 PM";
string formatForMySql = Convert.ToDateTime(latestscreentime);
未转换。也尝试过解析 还有
SimpleDateFormat from = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss tt");
SimpleDateFormat to = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = from.parse(latestscreentime); // 01/02/2014
String mysqlString = to.format(date);
这里的错误是
'SimpleDateFormat' could not be found (are you missing a using directive or an assembly reference?)
但我下载了 vjslib.dll
并添加 using SimpleDateFormat;
有人可以帮我解决这个错误吗?
最佳答案
首先将字符串更改为 DateTime
var latesttime = @"2/11/2015 8:04:06 PM";
DateTime dateValue = DateTime.Parse(latesttime);
现在你可以简单地做,
var sqlDateFormat= dateValue.ToString("yyyy-MM-dd HH:mm");
关于c# - 如何将字符串转换为mysql日期时间格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28464985/