Convert DateTime for MySQL using C#

后端 未结 4 1478
悲&欢浪女
悲&欢浪女 2020-11-27 16:32

I want to change the DateTime for MySQL in C#.

My MySQL database only accept this format 1976-04-09 22:10:00.

In C# have a string who have a dat

4条回答
  •  眼角桃花
    2020-11-27 17:23

    If your string format for the DateTime is fixed you can convert to the System.DateTime using:

    string myDate = "12-Apr-1976 22:10";
    DateTime dateValue = DateTime.Parse(myDate);
    

    Now, when you need it in your specific format, you can then reverse the process, i.e.:

    string formatForMySql = dateValue.ToString("yyyy-MM-dd HH:mm");
    

    edit - updated code. For some strange reason DateTime.ParseExact wasnt playing nice.

提交回复
热议问题