将C#中的DateTime转换为yyyy-MM-dd格式并将其存储到MySql DateTime字段中 [英] Convert DateTime in C# to yyyy-MM-dd format and Store it to MySql DateTime Field
问题描述
我正在尝试将DateTime
格式转换为yyyy-MM-dd
格式并将其存储到DateTime
对象.但这给了我MM/dd/yyyy
的系统DateTime
格式.
我正在使用以下代码进行转换.
I am trying to convert DateTime
format to yyyy-MM-dd
format and store it to DateTime
object. But it gives me the System DateTime
format that is MM/dd/yyyy
.
I am using following code to convert.
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.
我从一个应用程序获取DateTime并将该对象传递给另一应用程序,并且该应用程序将该日期存储在MySql的DateTime字段中,其格式为"yyyy-MM-dd".
这就是为什么我发布了这个问题.
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.
项目1具有我正在获取日期的课程. 处理器类是应用程序的中间件,它处理DateTime格式以转换为特定格式.并传递到使用DateTime并将其存储在MySql字段中的Other项目.
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.
推荐答案
使用DateTime.Now.ToString("yyyy-MM-dd h:mm tt");
.请参阅此.
这篇关于将C#中的DateTime转换为yyyy-MM-dd格式并将其存储到MySql DateTime字段中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!