使用C#转换日期时间为MySQL
问题描述:
我想改变日期时间为MySQL在C#。
I want to change the DateTime for MySQL in C#.
我的MySQL数据库只接受这种格式 1976年4月9日22点10分00秒
。
My MySQL database only accept this format 1976-04-09 22:10:00
.
在C#中有一个字符串谁拥有一个日期值:
In C# have a string who have a date value:
string str = "12-Apr-1976 22:10";
我想转换为MySQL则是这样的:
I want to convert for MySQL then it look like:
1976-04-12 22:10
如何我可以改变它们或如何其他程序员做到这一点,使用年月HH YY
的方法?谁能告诉我一下吗?
How I can change them or how other programmer do this by using dd mm hh yy
method? Can anyone tell me about them?
答
请记住,您可以硬code ISO格式
Keep in mind that you can hard-code ISO format
string formatForMySql = dateValue.ToString("yyyy-MM-dd HH:mm:ss");
或使用下一:
or use next:
// just to shorten the code
var isoDateTimeFormat = CultureInfo.InvariantCulture.DateTimeFormat;
// "1976-04-12T22:10:00"
dateValue.ToString(isoDateTimeFormat.SortableDateTimePattern);
// "1976-04-12 22:10:00Z"
dateValue.ToString(isoDateTimeFormat.UniversalSortableDateTimePattern)
等