将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
Convert DateTime in C# to yyyy-MM-dd format and Store it to MySql DateTime Field
提问by Rahul Gokani
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.
我正在尝试将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
回答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]