简体   繁体   中英

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.

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

But I download the vjslib.dll and add using SimpleDateFormat;

So could any one help me in solving this error?

First change your string to 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 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"

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 .

Instead of that, you can use custom date and time parsing with 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);
}

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. I think you are mixing it Java's SimpleDateFormat class .

In .NET Framework, usually DateTime.ToString method is used when you try to get string representation of your DateTime . 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 . That's why it might generate different results in different cultures.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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