2023-05-15 AM 8:57:06 ==> 2024-05-15 08:57:06.000 Type Convert

patrick

Well-known member
Joined
Dec 5, 2021
Messages
305
Programming Experience
1-3
Hello

C# string type = dt.Rows[0]["TIME"].ToString()
type is 2023-05-15 AM 8:57:06

I need to convert to MSSQL DateTime
MSSQL => 2023-05-15 08:57:06.000

If you do this, an error occurs. In C# , string.Format("{yyyy-MM-DD HH:mm:ss:fff}", "2023-05-15 AM 8:57:06");


I need to convert to DateTime, In C# 2023-05-15 AM 8:57:06 ====> 2024-05-15 08:57:06.000

******** How do you convert this(2023-05-15 AM 8:57:06 ==> 2024-05-15 08:57:06.000) type in C#?


Please Help me
 
Last edited:
C#:
 private void button1_Click(object sender, EventArgs e)
{
            string myStringDate = "2021/5/15";

            // Convert From String TO DateTime
            DateTime myDate = DateTime.Parse(myStringDate);

            // Convert From DateTime TO String
            string myConvertedDate = myDate.ToString("yyyy/MM/dd hh:mm:ss.fff");
}



I don't know if the above code is correct. howeverI think the above code solved it.
Thank you.
I solved it.
 
You still didn't answer the question. The question was what type of data does dt.Rows[0]["TIME"] contain? Instead you responded with what the value of that cell contains when converted to a string.

Anyway, if you know that the data value will always be that format, you can use DateTime.ParseExact() to convert the string value into a DateTime, and then just pass that DateTime into your SQL command as a parameter. You are using parameterized queries, right?
 

Latest posts

Back
Top Bottom