gpt4 book ai didi

c# - 如何将字符串转换为mysql日期时间格式?

转载 作者:行者123 更新时间:2023-11-29 23:01:27 24 4
gpt4 key购买 nike

我在将基于字符串的日期时间格式转换为 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com