如何将字符串转换为 mysql 日期时间格式?
How to convert string to mysql datetime format?
我在将基于字符串的日期时间格式转换为 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");
我知道有点晚了,但我还想指出其他几点..
首先,你不能 string
as;
string latesttime = "2\/11\/2015 8:04:06 PM";
因为它是一个 regular string literal, you need to escape your \
character since it is an escape sequence 字符。您可能想将其转义为 "2\/11\/2015 8:04:06 PM"
或使用 逐字字符串文字 作为 @"2\/11\/2015 8:04:06 PM"
我认为您的 Convert.ToDateTime()
永远不会 使用该字符串,因为 none CultureInfo
的 d\/MM\/yyyy h:mm:ss tt
格式为standard date and time format.
除此之外,您可以使用自定义日期和时间解析 DateTime.TryParseExact
method like;
string s = @"2\/11\/2015 8:04:06 PM";
DateTime dt;
if(DateTime.TryParseExact(s, @"d\/MM\/yyyy h:mm:ss tt",
CultureInfo.InvariantCulture,
DateTimeStyles.None,out dt))
{
Console.WriteLine(dt);
}
由于\
是自定义日期时间解析中的转义字符,需要用双斜杠转义为\
.
.NET Framework 中没有 SimpleDateFormat
class。我觉得你在混Java的SimpleDateFormat
class.
在 .NET Framework 中,当您尝试获取 DateTime
的字符串表示形式时,通常会使用 DateTime.ToString
method。解析后,您可以执行这些操作;
dt.ToString("dd/MM/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
// Result will be 02/11/2015 08:04:06 PM
dt.ToString("yyyy-MM-dd HH:mm:ss");
// Result will be 2015-11-02 20:04:06
我在第一个示例中使用 InvariantCulture
作为 IFormatProvider
,因为 The "/"
custom format specifier 具有特殊含义,因为 将我替换为当前文化或提供的文化日期分隔符。这就是为什么它可能在不同的文化中产生不同的结果。
我在将基于字符串的日期时间格式转换为 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");
我知道有点晚了,但我还想指出其他几点..
首先,你不能 string
as;
string latesttime = "2\/11\/2015 8:04:06 PM";
因为它是一个 regular string literal, you need to escape your \
character since it is an escape sequence 字符。您可能想将其转义为 "2\/11\/2015 8:04:06 PM"
或使用 逐字字符串文字 作为 @"2\/11\/2015 8:04:06 PM"
我认为您的 Convert.ToDateTime()
永远不会 使用该字符串,因为 none CultureInfo
的 d\/MM\/yyyy h:mm:ss tt
格式为standard date and time format.
除此之外,您可以使用自定义日期和时间解析 DateTime.TryParseExact
method like;
string s = @"2\/11\/2015 8:04:06 PM";
DateTime dt;
if(DateTime.TryParseExact(s, @"d\/MM\/yyyy h:mm:ss tt",
CultureInfo.InvariantCulture,
DateTimeStyles.None,out dt))
{
Console.WriteLine(dt);
}
由于\
是自定义日期时间解析中的转义字符,需要用双斜杠转义为\
.
.NET Framework 中没有 SimpleDateFormat
class。我觉得你在混Java的SimpleDateFormat
class.
在 .NET Framework 中,当您尝试获取 DateTime
的字符串表示形式时,通常会使用 DateTime.ToString
method。解析后,您可以执行这些操作;
dt.ToString("dd/MM/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
// Result will be 02/11/2015 08:04:06 PM
dt.ToString("yyyy-MM-dd HH:mm:ss");
// Result will be 2015-11-02 20:04:06
我在第一个示例中使用 InvariantCulture
作为 IFormatProvider
,因为 The "/"
custom format specifier 具有特殊含义,因为 将我替换为当前文化或提供的文化日期分隔符。这就是为什么它可能在不同的文化中产生不同的结果。