Convert DateTime for MySQL using C#

后端 未结 4 1507
悲&欢浪女
悲&欢浪女 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条回答
  •  猫巷女王i
    2020-11-27 17:00

    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)
    

    and so on

提交回复
热议问题