使用 C# 转换 MySQL 的日期时间 [英] Convert DateTime for MySQL using C#

查看:62
本文介绍了使用 C# 转换 MySQL 的日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 C# 中更改 MySQL 的 DateTime.

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

我的 MySQL 数据库只接受这种格式 1976-04-09 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

我如何更改它们或其他程序员如何使用 dd mm 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?

推荐答案

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

Keep in mind that you can hard-code ISO format

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# 转换 MySQL 的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆