gpt4 book ai didi

c# - 使用 C# 为 MySQL 转换 DateTime

转载 作者:IT老高 更新时间:2023-10-28 23:46:16 25 4
gpt4 key购买 nike

我想在 C# 中更改 MySQL 的日期时间。

我的 MySQL 数据库只接受这种格式 1976-04-09 22:10:00

在 C# 中有一个具有日期值的字符串:

string str = "12-Apr-1976 22:10";

我想为 MySQL 转换它看起来像:

1976-04-12 22:10

我如何更改它们或其他程序员如何使用 dd mm hh yy 方法来更改它们?谁能告诉我有关它们的信息?

最佳答案

请记住,您可以对 ISO 格式进行硬编码

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

或使用下一个:

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

等等

关于c# - 使用 C# 为 MySQL 转换 DateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3633262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com