将C#中的DateTime转换为yyyy-MM-dd格式并存入MySql DateTime字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19296433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:41:00  来源:igfitidea点击:

Convert DateTime in C# to yyyy-MM-dd format and Store it to MySql DateTime Field

c#mysqldatedatetime

提问by Rahul Gokani

I am trying to convert DateTimeformat to yyyy-MM-ddformat and store it to DateTimeobject. But it gives me the System DateTimeformat that is MM/dd/yyyy.
I am using following code to convert.

我正在尝试将DateTime格式转换为yyyy-MM-dd格式并将其存储到DateTime对象。但它给了我系统DateTime格式,即MM/dd/yyyy.
我正在使用以下代码进行转换。

string dateTime = DateTime.Now.ToString();
string createddate = Convert.ToDateTime(dateTime).ToString("yyyy-MM-dd h:mm tt");         
DateTime dt = DateTime.ParseExact(createddate, "yyyy-MM-dd h:mm tt",CultureInfo.InvariantCulture);

but non of the above line converts into the specified format.
Can any one help to solve this.

但以上行的非转换为指定的格式。
任何人都可以帮助解决这个问题。

I am getting the DateTime from one application and passing this object to other application and That application is storing that date into MySql's DateTime field which is in the format "yyyy-MM-dd".
This is why I have posted this question.

我从一个应用程序获取 DateTime 并将此对象传递给另一个应用程序,该应用程序将该日期存储到 MySql 的 DateTime 字段中,该字段的格式为“yyyy-MM-dd”。
这就是我发布这个问题的原因。

Project 1 has class from that I am getting the date. and the processor class which is the middle ware of the application it processes the DateTime format to convert in specific format. And passes to the Other project which consumes the DateTime and stores that in the MySql field.

项目 1 有课程,我正在获取日期。处理器类是应用程序的中间件,它处理 DateTime 格式以转换为特定格式。并传递给使用 DateTime 并将其存储在 MySql 字段中的其他项目。

采纳答案by Alex

Use DateTime.Now.ToString("yyyy-MM-dd h:mm tt");. See this.

使用DateTime.Now.ToString("yyyy-MM-dd h:mm tt");. 看到这个

回答by DrewbieDoo

Have you tried?

你有没有尝试过?

var isoDateTimeFormat = CultureInfo.InvariantCulture.DateTimeFormat;

// "2013-10-10T22:10:00"
 dateValue.ToString(isoDateTimeFormat.SortableDateTimePattern); 

// "2013-10-10 22:10:00Z"    
dateValue.ToString(isoDateTimeFormat.UniversalSortableDateTimePattern)

Also try using parameters when you store the c# datetime value in the mySql database, this might help.

当您将 c# 日期时间值存储在 mySql 数据库中时,也可以尝试使用参数,这可能会有所帮助。

回答by sanjay.arora29

We can use the below its very simple.

我们可以使用下面的非常简单。

Date.ToString("yyyy-MM-dd");

回答by Asanka Madushan

Try setting a custom CultureInfo for CurrentCulture and CurrentUICulture.

尝试为 CurrentCulture 和 CurrentUICulture 设置自定义 CultureInfo。

Globalization.CultureInfo customCulture = new Globalization.CultureInfo("en-US", true);

customCulture.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd h:mm tt";

System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;
System.Threading.Thread.CurrentThread.CurrentUICulture = customCulture;

DateTime newDate = System.Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd h:mm tt"));

回答by YKC

GetDateTimeFormats can parse DateTime to different formats. Example to "yyyy-MM-dd" format.

GetDateTimeFormats 可以将 DateTime 解析为不同的格式。示例为“yyyy-MM-dd”格式。

SomeDate.Value.GetDateTimeFormats()[5]

GetDateTimeFormats

获取日期时间格式