简体   繁体   English

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

[英]How to convert string to mysql datetime format?

I am facing a difficulty in converting a string based datetime format to mysql date time format. 我在将基于字符串的日期时间格式转换为mysql日期时间格式时遇到了困难。

I tried the following 我尝试了以下内容

str latesttime =  "2\/11\/2015 8:04:06 PM";
string formatForMySql = Convert.ToDateTime(latestscreentime);

Not converted. 不转换。 also tried with parse And also 也试过解析而且

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); 

here error is 这里的错误是

'SimpleDateFormat' could not be found (are you missing a using directive or an assembly reference?) 找不到“ SimpleDateFormat”(您是否缺少using指令或程序集引用?)

But I download the vjslib.dll and add using SimpleDateFormat; 但是我下载了vjslib.dllusing SimpleDateFormat;添加using SimpleDateFormat;

So could any one help me in solving this error? 那么任何人都可以帮助我解决这个错误吗?

First change your string to DateTime 首先将您的字符串更改为DateTime

var latesttime = @"2/11/2015 8:04:06 PM";
DateTime dateValue = DateTime.Parse(latesttime);

Now you can simply do, 现在您可以简单地做

var sqlDateFormat= dateValue.ToString("yyyy-MM-dd HH:mm");

I know it is a little bit too late, but I want to point some other points also.. 我知道这有点太晚了,但我还想指出其他一点......

First of all, you can't have a string as; 首先,你不能有一个string ;

string latesttime = "2\/11\/2015 8:04:06 PM";

because since it is a regular string literal , you need to escape your \\ character since it is an escape sequence character. 因为它是一个常规的字符串文字 ,所以您需要对\\字符进行转义,因为它是一个转义序列字符。 You might wanna escape it as "2\\\\/11\\\\/2015 8:04:06 PM" or use verbatim string literal as @"2\\/11\\/2015 8:04:06 PM" 您可能想将其转义为"2\\\\/11\\\\/2015 8:04:06 PM"或使用逐字字符串文字作为@"2\\/11\\/2015 8:04:06 PM"

I don't think your Convert.ToDateTime() ever work with that string because none CultureInfo have d\\\\/MM\\\\/yyyy h:mm:ss tt format as a standard date and time format . 我不认为你的Convert.ToDateTime() 曾经与该字符串的工作,因为没有任何CultureInfo已经d\\\\/MM\\\\/yyyy h:mm:ss tt格式为标准的日期和时间格式

Instead of that, you can use custom date and time parsing with DateTime.TryParseExact method like; 取而代之的是,您可以对DateTime.TryParseExact方法使用自定义日期和时间解析,例如;

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);
}

Since \\ is an escape character in custom date and time parsing, you need to escape with double slash as \\\\ . 由于\\是自定义日期和时间解析中的转义字符,因此您需要使用\\\\进行双斜杠转义。

There is no SimpleDateFormat class in .NET Framework. .NET Framework中没有SimpleDateFormat类。 I think you are mixing it Java's SimpleDateFormat class . 我认为您正在将其混入Java的SimpleDateFormat

In .NET Framework, usually DateTime.ToString method is used when you try to get string representation of your DateTime . 在.NET Framework中,当您尝试获取DateTime字符串表示形式时, 通常使用 DateTime.ToString方法 After you parsing, you can do these; 解析后,您可以执行这些操作;

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

I used InvariantCulture as an IFormatProvider in first example because The "/" custom format specifier has a special meaning as replace me with current culture or supplied culture date separator . 我在第一个例子中使用InvariantCulture作为IFormatProvider ,因为"/"自定义格式说明符具有特殊含义,因为用当前文化或提供的文化日期分隔符替换我 That's why it might generate different results in different cultures. 这就是为什么它可能在不同的文化中产生不同的结果。

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

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